Esempio n. 1
0
 public JSHandle(ExecutionContext context, Session client, object remoteObject)
 {
     _context     = context;
     _client      = client;
     RemoteObject = remoteObject;
 }
 /// <summary>
 /// A utility function to be used with <see cref="Extensions.EvaluateFunctionAsync{T}(Task{JSHandle}, string, object[])"/>
 /// </summary>
 /// <param name="selector">A selector to query element for</param>
 /// <returns>Task which resolves to a <see cref="JSHandle"/> of <c>document.querySelectorAll</c> result</returns>
 public Task <JSHandle> QuerySelectorAllHandleAsync(string selector)
 => ExecutionContext.EvaluateFunctionHandleAsync(
     "(element, selector) => Array.from(element.querySelectorAll(selector))", this, selector);
Esempio n. 3
0
 internal JSHandle(ExecutionContext context, CDPSession client, RemoteObject remoteObject)
 {
     ExecutionContext = context;
     Client           = client;
     RemoteObject     = remoteObject;
 }
        /// <summary>
        /// This method scrolls element into view if needed, and then uses <seealso cref="Page.ScreenshotBase64Async(ScreenshotOptions)"/> to take a screenshot of the element.
        /// If the element is detached from DOM, the method throws an error.
        /// </summary>
        /// <returns>Task which resolves to a <see cref="string"/> containing the image data as base64.</returns>
        /// <param name="options">Screenshot options.</param>
        public async Task <string> ScreenshotBase64Async(ScreenshotOptions options)
        {
            var needsViewportReset = false;
            var boundingBox        = await BoundingBoxAsync().ConfigureAwait(false);

            if (boundingBox == null)
            {
                throw new PuppeteerException("Node is either not visible or not an HTMLElement");
            }

            var viewport = Page.Viewport;

            if (viewport != null && (boundingBox.Width > viewport.Width || boundingBox.Height > viewport.Height))
            {
                var newRawViewport = JObject.FromObject(viewport);
                newRawViewport.Merge(new ViewPortOptions
                {
                    Width  = (int)Math.Max(viewport.Width, Math.Ceiling(boundingBox.Width)),
                    Height = (int)Math.Max(viewport.Height, Math.Ceiling(boundingBox.Height))
                });
                await Page.SetViewportAsync(newRawViewport.ToObject <ViewPortOptions>(true)).ConfigureAwait(false);

                needsViewportReset = true;
            }
            await ExecutionContext.EvaluateFunctionAsync(@"function(element) {
                element.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant'});
            }", this).ConfigureAwait(false);

            await ScrollIntoViewIfNeededAsync().ConfigureAwait(false);

            boundingBox = await BoundingBoxAsync().ConfigureAwait(false);

            if (boundingBox == null)
            {
                throw new PuppeteerException("Node is either not visible or not an HTMLElement");
            }
            if (boundingBox.Width == 0)
            {
                throw new PuppeteerException("Node has 0 width.");
            }
            if (boundingBox.Height == 0)
            {
                throw new PuppeteerException("Node has 0 height.");
            }
            var getLayoutMetricsResponse = await Client.SendAsync <GetLayoutMetricsResponse>("Page.getLayoutMetrics").ConfigureAwait(false);

            var clip = boundingBox;

            clip.X += getLayoutMetricsResponse.LayoutViewport.PageX;
            clip.Y += getLayoutMetricsResponse.LayoutViewport.PageY;

            options.Clip = boundingBox.ToClip();
            var imageData = await Page.ScreenshotBase64Async(options).ConfigureAwait(false);

            if (needsViewportReset)
            {
                await Page.SetViewportAsync(viewport).ConfigureAwait(false);
            }

            return(imageData);
        }