How to use PageAddScriptToEvaluateOnNewDocumentRequest class of PuppeteerSharp.Messaging package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Messaging.PageAddScriptToEvaluateOnNewDocumentRequest

Page.cs

Source:Page.cs Github

copy

Full Screen

...525        /// <returns>Task</returns>526        public Task EvaluateFunctionOnNewDocumentAsync(string pageFunction, params object[] args)527        {528            var source = EvaluationString(pageFunction, args);529            return Client.SendAsync("Page.addScriptToEvaluateOnNewDocument", new PageAddScriptToEvaluateOnNewDocumentRequest530            {531                Source = source532            });533        }534        /// <summary>535        /// Adds a function which would be invoked in one of the following scenarios:536        /// - whenever the page is navigated537        /// - whenever the child frame is attached or navigated. In this case, the function is invoked in the context of the newly attached frame538        /// </summary>539        /// <param name="expression">Javascript expression to be evaluated in browser context</param>540        /// <remarks>541        /// The function is invoked after the document was created but before any of its scripts were run. This is useful to amend JavaScript environment, e.g. to seed <c>Math.random</c>.542        /// </remarks>543        /// <example>544        /// An example of overriding the navigator.languages property before the page loads:545        /// <code>546        /// await page.EvaluateExpressionOnNewDocumentAsync("window.__example = true;");547        /// </code>548        /// </example>549        /// <returns>Task</returns>550        public Task EvaluateExpressionOnNewDocumentAsync(string expression)551            => Client.SendAsync("Page.addScriptToEvaluateOnNewDocument", new PageAddScriptToEvaluateOnNewDocumentRequest552            {553                Source = expression554            });555        /// <summary>556        /// The method iterates JavaScript heap and finds all the objects with the given prototype.557        /// Shortcut for <c>page.MainFrame.GetExecutionContextAsync().QueryObjectsAsync(prototypeHandle)</c>.558        /// </summary>559        /// <returns>A task which resolves to a handle to an array of objects with this prototype.</returns>560        /// <param name="prototypeHandle">A handle to the object prototype.</param>561        public async Task<JSHandle> QueryObjectsAsync(JSHandle prototypeHandle)562        {563            var context = await MainFrame.GetExecutionContextAsync().ConfigureAwait(false);564            return await context.QueryObjectsAsync(prototypeHandle).ConfigureAwait(false);565        }566        /// <summary>567        /// Activating request interception enables <see cref="PuppeteerSharp.Request.AbortAsync(RequestAbortErrorCode)">request.AbortAsync</see>,568        /// <see cref="PuppeteerSharp.Request.ContinueAsync(Payload)">request.ContinueAsync</see> and <see cref="PuppeteerSharp.Request.RespondAsync(ResponseData)">request.RespondAsync</see> methods.569        /// </summary>570        /// <returns>The request interception task.</returns>571        /// <param name="value">Whether to enable request interception..</param>572        public Task SetRequestInterceptionAsync(bool value)573            => FrameManager.NetworkManager.SetRequestInterceptionAsync(value);574        /// <summary>575        /// Set offline mode for the page.576        /// </summary>577        /// <returns>Result task</returns>578        /// <param name="value">When <c>true</c> enables offline mode for the page.</param>579        public Task SetOfflineModeAsync(bool value) => FrameManager.NetworkManager.SetOfflineModeAsync(value);580        /// <summary>581        /// Emulates network conditions582        /// </summary>583        /// <param name="networkConditions">Passing <c>null</c> disables network condition emulation.</param>584        /// <returns>Result task</returns>585        /// <remarks>586        /// **NOTE** This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644)587        /// </remarks>588        public Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions) => FrameManager.NetworkManager.EmulateNetworkConditionsAsync(networkConditions);589        /// <summary>590        /// Returns the page's cookies591        /// </summary>592        /// <param name="urls">Url's to return cookies for</param>593        /// <returns>Array of cookies</returns>594        /// <remarks>595        /// If no URLs are specified, this method returns cookies for the current page URL.596        /// If URLs are specified, only cookies for those URLs are returned.597        /// </remarks>598        public async Task<CookieParam[]> GetCookiesAsync(params string[] urls)599            => (await Client.SendAsync<NetworkGetCookiesResponse>("Network.getCookies", new NetworkGetCookiesRequest600            {601                Urls = urls.Length > 0 ? urls : new string[] { Url }602            }).ConfigureAwait(false)).Cookies;603        /// <summary>604        /// Clears all of the current cookies and then sets the cookies for the page605        /// </summary>606        /// <param name="cookies">Cookies to set</param>607        /// <returns>Task</returns>608        public async Task SetCookieAsync(params CookieParam[] cookies)609        {610            foreach (var cookie in cookies)611            {612                if (string.IsNullOrEmpty(cookie.Url) && Url.StartsWith("http", StringComparison.Ordinal))613                {614                    cookie.Url = Url;615                }616                if (cookie.Url == "about:blank")617                {618                    throw new PuppeteerException($"Blank page can not have cookie \"{cookie.Name}\"");619                }620            }621            await DeleteCookieAsync(cookies).ConfigureAwait(false);622            if (cookies.Length > 0)623            {624                await Client.SendAsync("Network.setCookies", new NetworkSetCookiesRequest625                {626                    Cookies = cookies627                }).ConfigureAwait(false);628            }629        }630        /// <summary>631        /// Deletes cookies from the page632        /// </summary>633        /// <param name="cookies">Cookies to delete</param>634        /// <returns>Task</returns>635        public async Task DeleteCookieAsync(params CookieParam[] cookies)636        {637            var pageURL = Url;638            foreach (var cookie in cookies)639            {640                if (string.IsNullOrEmpty(cookie.Url) && pageURL.StartsWith("http", StringComparison.Ordinal))641                {642                    cookie.Url = pageURL;643                }644                await Client.SendAsync("Network.deleteCookies", cookie).ConfigureAwait(false);645            }646        }647        /// <summary>648        /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content649        /// </summary>650        /// <param name="options">add script tag options</param>651        /// <remarks>652        /// Shortcut for <c>page.MainFrame.AddScriptTagAsync(options)</c>653        /// </remarks>654        /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>655        /// <seealso cref="Frame.AddScriptTagAsync(AddTagOptions)"/>656        public Task<ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainFrame.AddScriptTagAsync(options);657        /// <summary>658        /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content659        /// </summary>660        /// <param name="url">script url</param>661        /// <remarks>662        /// Shortcut for <c>page.MainFrame.AddScriptTagAsync(new AddTagOptions { Url = url })</c>663        /// </remarks>664        /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>665        public Task<ElementHandle> AddScriptTagAsync(string url) => AddScriptTagAsync(new AddTagOptions { Url = url });666        /// <summary>667        /// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content668        /// </summary>669        /// <param name="options">add style tag options</param>670        /// <remarks>671        /// Shortcut for <c>page.MainFrame.AddStyleTagAsync(options)</c>672        /// </remarks>673        /// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>674        /// <seealso cref="Frame.AddStyleTag(AddTagOptions)"/>675        public Task<ElementHandle> AddStyleTagAsync(AddTagOptions options) => MainFrame.AddStyleTagAsync(options);676        /// <summary>677        /// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content678        /// </summary>679        /// <param name="url">stylesheel url</param>680        /// <remarks>681        /// Shortcut for <c>page.MainFrame.AddStyleTagAsync(new AddTagOptions { Url = url })</c>682        /// </remarks>683        /// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>684        public Task<ElementHandle> AddStyleTagAsync(string url) => AddStyleTagAsync(new AddTagOptions { Url = url });685        /// <summary>686        /// Adds a function called <c>name</c> on the page's <c>window</c> object.687        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves when <paramref name="puppeteerFunction"/> completes.688        /// </summary>689        /// <param name="name">Name of the function on the window object</param>690        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>691        /// <remarks>692        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.693        /// Functions installed via <see cref="ExposeFunctionAsync(string, Action)"/> survive navigations694        /// </remarks>695        /// <returns>Task</returns>696        public Task ExposeFunctionAsync(string name, Action puppeteerFunction)697            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);698        /// <summary>699        /// Adds a function called <c>name</c> on the page's <c>window</c> object.700        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.701        /// </summary>702        /// <typeparam name="TResult">The result of <paramref name="puppeteerFunction"/></typeparam>703        /// <param name="name">Name of the function on the window object</param>704        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>705        /// <remarks>706        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.707        /// Functions installed via <see cref="ExposeFunctionAsync{TResult}(string, Func{TResult})"/> survive navigations708        /// </remarks>709        /// <returns>Task</returns>710        public Task ExposeFunctionAsync<TResult>(string name, Func<TResult> puppeteerFunction)711            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);712        /// <summary>713        /// Adds a function called <c>name</c> on the page's <c>window</c> object.714        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.715        /// </summary>716        /// <typeparam name="T">The parameter of <paramref name="puppeteerFunction"/></typeparam>717        /// <typeparam name="TResult">The result of <paramref name="puppeteerFunction"/></typeparam>718        /// <param name="name">Name of the function on the window object</param>719        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>720        /// <remarks>721        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.722        /// Functions installed via <see cref="ExposeFunctionAsync{T, TResult}(string, Func{T, TResult})"/> survive navigations723        /// </remarks>724        /// <returns>Task</returns>725        public Task ExposeFunctionAsync<T, TResult>(string name, Func<T, TResult> puppeteerFunction)726            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);727        /// <summary>728        /// Adds a function called <c>name</c> on the page's <c>window</c> object.729        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.730        /// </summary>731        /// <typeparam name="T1">The first parameter of <paramref name="puppeteerFunction"/></typeparam>732        /// <typeparam name="T2">The second parameter of <paramref name="puppeteerFunction"/></typeparam>733        /// <typeparam name="TResult">The result of <paramref name="puppeteerFunction"/></typeparam>734        /// <param name="name">Name of the function on the window object</param>735        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>736        /// <remarks>737        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.738        /// Functions installed via <see cref="ExposeFunctionAsync{T1, T2, TResult}(string, Func{T1, T2, TResult})"/> survive navigations739        /// </remarks>740        /// <returns>Task</returns>741        public Task ExposeFunctionAsync<T1, T2, TResult>(string name, Func<T1, T2, TResult> puppeteerFunction)742            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);743        /// <summary>744        /// Adds a function called <c>name</c> on the page's <c>window</c> object.745        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.746        /// </summary>747        /// <typeparam name="T1">The first parameter of <paramref name="puppeteerFunction"/></typeparam>748        /// <typeparam name="T2">The second parameter of <paramref name="puppeteerFunction"/></typeparam>749        /// <typeparam name="T3">The third parameter of <paramref name="puppeteerFunction"/></typeparam>750        /// <typeparam name="TResult">The result of <paramref name="puppeteerFunction"/></typeparam>751        /// <param name="name">Name of the function on the window object</param>752        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>753        /// <remarks>754        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.755        /// Functions installed via <see cref="ExposeFunctionAsync{T1, T2, T3, TResult}(string, Func{T1, T2, T3, TResult})"/> survive navigations756        /// </remarks>757        /// <returns>Task</returns>758        public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> puppeteerFunction)759            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);760        /// <summary>761        /// Adds a function called <c>name</c> on the page's <c>window</c> object.762        /// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.763        /// </summary>764        /// <typeparam name="T1">The first parameter of <paramref name="puppeteerFunction"/></typeparam>765        /// <typeparam name="T2">The second parameter of <paramref name="puppeteerFunction"/></typeparam>766        /// <typeparam name="T3">The third parameter of <paramref name="puppeteerFunction"/></typeparam>767        /// <typeparam name="T4">The fourth parameter of <paramref name="puppeteerFunction"/></typeparam>768        /// <typeparam name="TResult">The result of <paramref name="puppeteerFunction"/></typeparam>769        /// <param name="name">Name of the function on the window object</param>770        /// <param name="puppeteerFunction">Callback function which will be called in Puppeteer's context.</param>771        /// <remarks>772        /// If the <paramref name="puppeteerFunction"/> returns a <see cref="Task"/>, it will be awaited.773        /// Functions installed via <see cref="ExposeFunctionAsync{T1, T2, T3, T4, TResult}(string, Func{T1, T2, T3, T4, TResult})"/> survive navigations774        /// </remarks>775        /// <returns>Task</returns>776        public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> puppeteerFunction)777            => ExposeFunctionAsync(name, (Delegate)puppeteerFunction);778        /// <summary>779        /// Gets the full HTML contents of the page, including the doctype.780        /// </summary>781        /// <returns>Task which resolves to the HTML content.</returns>782        /// <seealso cref="Frame.GetContentAsync"/>783        public Task<string> GetContentAsync() => FrameManager.MainFrame.GetContentAsync();784        /// <summary>785        /// Sets the HTML markup to the page786        /// </summary>787        /// <param name="html">HTML markup to assign to the page.</param>788        /// <param name="options">The navigations options</param>789        /// <returns>Task.</returns>790        /// <seealso cref="Frame.SetContentAsync(string, NavigationOptions)"/>791        public Task SetContentAsync(string html, NavigationOptions options = null) => FrameManager.MainFrame.SetContentAsync(html, options);792        /// <summary>793        /// Navigates to an url794        /// </summary>795        /// <remarks>796        /// <see cref="GoToAsync(string, int?, WaitUntilNavigation[])"/> will throw an error if:797        /// - there's an SSL error (e.g. in case of self-signed certificates).798        /// - target URL is invalid.799        /// - the `timeout` is exceeded during navigation.800        /// - the remote server does not respond or is unreachable.801        /// - the main resource failed to load.802        ///803        /// <see cref="GoToAsync(string, int?, WaitUntilNavigation[])"/> will not throw an error when any valid HTTP status code is returned by the remote server,804        /// including 404 "Not Found" and 500 "Internal Server Error".  The status code for such responses can be retrieved by calling <see cref="PuppeteerSharp.Response.Status"/>805        ///806        /// > **NOTE** <see cref="GoToAsync(string, int?, WaitUntilNavigation[])"/> either throws an error or returns a main resource response.807        /// The only exceptions are navigation to `about:blank` or navigation to the same URL with a different hash, which would succeed and return `null`.808        ///809        /// > **NOTE** Headless mode doesn't support navigation to a PDF document. See the <see fref="https://bugs.chromium.org/p/chromium/issues/detail?id=761295">upstream issue</see>.810        ///811        /// Shortcut for <seealso cref="Frame.GoToAsync(string, int?, WaitUntilNavigation[])"/>812        /// </remarks>813        /// <param name="url">URL to navigate page to. The url should include scheme, e.g. https://.</param>814        /// <param name="options">Navigation parameters.</param>815        /// <returns>Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect.</returns>816        /// <seealso cref="GoToAsync(string, int?, WaitUntilNavigation[])"/>817        public Task<Response> GoToAsync(string url, NavigationOptions options) => FrameManager.MainFrame.GoToAsync(url, options);818        /// <summary>819        /// Navigates to an url820        /// </summary>821        /// <param name="url">URL to navigate page to. The url should include scheme, e.g. https://.</param>822        /// <param name="timeout">Maximum navigation time in milliseconds, defaults to 30 seconds, pass <c>0</c> to disable timeout. </param>823        /// <param name="waitUntil">When to consider navigation succeeded, defaults to <see cref="WaitUntilNavigation.Load"/>. Given an array of <see cref="WaitUntilNavigation"/>, navigation is considered to be successful after all events have been fired</param>824        /// <returns>Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect</returns>825        /// <seealso cref="GoToAsync(string, NavigationOptions)"/>826        public Task<Response> GoToAsync(string url, int? timeout = null, WaitUntilNavigation[] waitUntil = null)827            => GoToAsync(url, new NavigationOptions { Timeout = timeout, WaitUntil = waitUntil });828        /// <summary>829        /// Navigates to an url830        /// </summary>831        /// <param name="url">URL to navigate page to. The url should include scheme, e.g. https://.</param>832        /// <param name="waitUntil">When to consider navigation succeeded.</param>833        /// <returns>Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect</returns>834        /// <seealso cref="GoToAsync(string, NavigationOptions)"/>835        public Task<Response> GoToAsync(string url, WaitUntilNavigation waitUntil)836            => GoToAsync(url, new NavigationOptions { WaitUntil = new[] { waitUntil } });837        /// <summary>838        /// generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>839        /// </summary>840        /// <param name="file">The file path to save the PDF to. paths are resolved using <see cref="Path.GetFullPath(string)"/></param>841        /// <returns></returns>842        /// <remarks>843        /// Generating a pdf is currently only supported in Chrome headless844        /// </remarks>845        public Task PdfAsync(string file) => PdfAsync(file, new PdfOptions());846        /// <summary>847        ///  generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>848        /// </summary>849        /// <param name="file">The file path to save the PDF to. paths are resolved using <see cref="Path.GetFullPath(string)"/></param>850        /// <param name="options">pdf options</param>851        /// <returns></returns>852        /// <remarks>853        /// Generating a pdf is currently only supported in Chrome headless854        /// </remarks>855        public async Task PdfAsync(string file, PdfOptions options)856        {857            if (options == null)858            {859                throw new ArgumentNullException(nameof(options));860            }861            await PdfInternalAsync(file, options).ConfigureAwait(false);862        }863        /// <summary>864        /// generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>865        /// </summary>866        /// <returns>Task which resolves to a <see cref="Stream"/> containing the PDF data.</returns>867        /// <remarks>868        /// Generating a pdf is currently only supported in Chrome headless869        /// </remarks>870        public Task<Stream> PdfStreamAsync() => PdfStreamAsync(new PdfOptions());871        /// <summary>872        /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>873        /// </summary>874        /// <param name="options">pdf options</param>875        /// <returns>Task which resolves to a <see cref="Stream"/> containing the PDF data.</returns>876        /// <remarks>877        /// Generating a pdf is currently only supported in Chrome headless878        /// </remarks>879        public async Task<Stream> PdfStreamAsync(PdfOptions options)880            => new MemoryStream(await PdfDataAsync(options).ConfigureAwait(false));881        /// <summary>882        /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>883        /// </summary>884        /// <returns>Task which resolves to a <see cref="byte"/>[] containing the PDF data.</returns>885        /// <remarks>886        /// Generating a pdf is currently only supported in Chrome headless887        /// </remarks>888        public Task<byte[]> PdfDataAsync() => PdfDataAsync(new PdfOptions());889        /// <summary>890        /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>891        /// </summary>892        /// <param name="options">pdf options</param>893        /// <returns>Task which resolves to a <see cref="byte"/>[] containing the PDF data.</returns>894        /// <remarks>895        /// Generating a pdf is currently only supported in Chrome headless896        /// </remarks>897        public Task<byte[]> PdfDataAsync(PdfOptions options)898        {899            if (options == null)900            {901                throw new ArgumentNullException(nameof(options));902            }903            return PdfInternalAsync(null, options);904        }905        internal async Task<byte[]> PdfInternalAsync(string file, PdfOptions options)906        {907            var paperWidth = PaperFormat.Letter.Width;908            var paperHeight = PaperFormat.Letter.Height;909            if (options.Format != null)910            {911                paperWidth = options.Format.Width;912                paperHeight = options.Format.Height;913            }914            else915            {916                if (options.Width != null)917                {918                    paperWidth = ConvertPrintParameterToInches(options.Width);919                }920                if (options.Height != null)921                {922                    paperHeight = ConvertPrintParameterToInches(options.Height);923                }924            }925            var marginTop = ConvertPrintParameterToInches(options.MarginOptions.Top);926            var marginLeft = ConvertPrintParameterToInches(options.MarginOptions.Left);927            var marginBottom = ConvertPrintParameterToInches(options.MarginOptions.Bottom);928            var marginRight = ConvertPrintParameterToInches(options.MarginOptions.Right);929            if (options.OmitBackground)930            {931                await SetTransparentBackgroundColorAsync().ConfigureAwait(false);932            }933            var result = await Client.SendAsync<PagePrintToPDFResponse>("Page.printToPDF", new PagePrintToPDFRequest934            {935                TransferMode = "ReturnAsStream",936                Landscape = options.Landscape,937                DisplayHeaderFooter = options.DisplayHeaderFooter,938                HeaderTemplate = options.HeaderTemplate,939                FooterTemplate = options.FooterTemplate,940                PrintBackground = options.PrintBackground,941                Scale = options.Scale,942                PaperWidth = paperWidth,943                PaperHeight = paperHeight,944                MarginTop = marginTop,945                MarginBottom = marginBottom,946                MarginLeft = marginLeft,947                MarginRight = marginRight,948                PageRanges = options.PageRanges,949                PreferCSSPageSize = options.PreferCSSPageSize950            }).ConfigureAwait(false);951            if (options.OmitBackground)952            {953                await ResetDefaultBackgroundColorAsync().ConfigureAwait(false);954            }955            return await ProtocolStreamReader.ReadProtocolStreamByteAsync(Client, result.Stream, file).ConfigureAwait(false);956        }957        /// <summary>958        /// Enables/Disables Javascript on the page959        /// </summary>960        /// <returns>Task.</returns>961        /// <param name="enabled">Whether or not to enable JavaScript on the page.</param>962        public Task SetJavaScriptEnabledAsync(bool enabled)963        {964            if (enabled == JavascriptEnabled)965            {966                return Task.CompletedTask;967            }968            JavascriptEnabled = enabled;969            return Client.SendAsync("Emulation.setScriptExecutionDisabled", new EmulationSetScriptExecutionDisabledRequest970            {971                Value = !enabled972            });973        }974        /// <summary>975        /// Toggles bypassing page's Content-Security-Policy.976        /// </summary>977        /// <param name="enabled">sets bypassing of page's Content-Security-Policy.</param>978        /// <returns></returns>979        /// <remarks>980        /// CSP bypassing happens at the moment of CSP initialization rather then evaluation.981        /// Usually this means that <see cref="SetBypassCSPAsync(bool)"/> should be called before navigating to the domain.982        /// </remarks>983        public Task SetBypassCSPAsync(bool enabled) => Client.SendAsync("Page.setBypassCSP", new PageSetBypassCSPRequest984        {985            Enabled = enabled986        });987        /// <summary>988        /// Emulates a media such as screen or print.989        /// </summary>990        /// <returns>Task.</returns>991        /// <param name="media">Media to set.</param>992        [Obsolete("User EmulateMediaTypeAsync instead")]993        public Task EmulateMediaAsync(MediaType media) => EmulateMediaTypeAsync(media);994        /// <summary>995        /// Emulates a media such as screen or print.996        /// </summary>997        /// <param name="type">Media to set.</param>998        /// <example>999        /// <code>1000        /// <![CDATA[1001        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");1002        /// // → true1003        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");1004        /// // → true1005        /// await page.EmulateMediaTypeAsync(MediaType.Print);1006        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");1007        /// // → false1008        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");1009        /// // → true1010        /// await page.EmulateMediaTypeAsync(MediaType.None);1011        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");1012        /// // → true1013        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");1014        /// // → true1015        /// ]]>1016        /// </code>1017        /// </example>1018        /// <returns>Emulate media type task.</returns>1019        public Task EmulateMediaTypeAsync(MediaType type)1020            => Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaTypeRequest { Media = type });1021        /// <summary>1022        /// Given an array of media feature objects, emulates CSS media features on the page.1023        /// </summary>1024        /// <param name="features">Features to apply</param>1025        /// <example>1026        /// <code>1027        /// <![CDATA[1028        /// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature =  MediaFeature.PrefersColorScheme, Value = "dark" }});1029        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");1030        /// // → true1031        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");1032        /// // → false1033        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");1034        /// // → false1035        /// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" }});1036        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");1037        /// // → true1038        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");1039        /// // → false1040        /// await page.EmulateMediaFeaturesAsync(new MediaFeature[]1041        /// {1042        ///   new MediaFeature { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" },1043        ///   new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },1044        /// });1045        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");1046        /// // → true1047        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");1048        /// // → false1049        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");1050        /// // → false1051        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");1052        /// // → true1053        /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");1054        /// // → false1055        /// ]]>1056        /// </code>1057        /// </example>1058        /// <returns>Emulate features task</returns>1059        public Task EmulateMediaFeaturesAsync(IEnumerable<MediaFeatureValue> features)1060            => Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaFeatureRequest { Features = features });1061        /// <summary>1062        /// Sets the viewport.1063        /// In the case of multiple pages in a single browser, each page can have its own viewport size.1064        /// <see cref="SetViewportAsync(ViewPortOptions)"/> will resize the page. A lot of websites don't expect phones to change size, so you should set the viewport before navigating to the page.1065        /// </summary>1066        /// <example>1067        ///<![CDATA[1068        /// using(var page = await browser.NewPageAsync())1069        /// {1070        ///     await page.SetViewPortAsync(new ViewPortOptions1071        ///     {1072        ///         Width = 640,1073        ///         Height = 480,1074        ///         DeviceScaleFactor = 11075        ///     });1076        ///     await page.goto('https://www.example.com');1077        /// }1078        /// ]]>1079        /// </example>1080        /// <returns>The viewport task.</returns>1081        /// <param name="viewport">Viewport options.</param>1082        public async Task SetViewportAsync(ViewPortOptions viewport)1083        {1084            if (viewport == null)1085            {1086                throw new ArgumentNullException(nameof(viewport));1087            }1088            var needsReload = await _emulationManager.EmulateViewport(viewport).ConfigureAwait(false);1089            Viewport = viewport;1090            if (needsReload)1091            {1092                await ReloadAsync().ConfigureAwait(false);1093            }1094        }1095        /// <summary>1096        /// Emulates given device metrics and user agent.1097        /// </summary>1098        /// <remarks>1099        /// This method is a shortcut for calling two methods:1100        /// <see cref="SetViewportAsync(ViewPortOptions)"/>1101        /// <see cref="SetUserAgentAsync(string)"/>1102        /// To aid emulation, puppeteer provides a list of device descriptors which can be obtained via the <see cref="Puppeteer.Devices"/>.1103        /// <see cref="EmulateAsync(DeviceDescriptor)"/> will resize the page. A lot of websites don't expect phones to change size, so you should emulate before navigating to the page.1104        /// </remarks>1105        /// <example>1106        ///<![CDATA[1107        /// var iPhone = Puppeteer.Devices[DeviceDescriptorName.IPhone6];1108        /// using(var page = await browser.NewPageAsync())1109        /// {1110        ///     await page.EmulateAsync(iPhone);1111        ///     await page.goto('https://www.google.com');1112        /// }1113        /// ]]>1114        /// </example>1115        /// <returns>Task.</returns>1116        /// <param name="options">Emulation options.</param>1117        public Task EmulateAsync(DeviceDescriptor options)1118        {1119            if (options == null)1120            {1121                throw new ArgumentNullException(nameof(options));1122            }1123            return Task.WhenAll(1124                SetViewportAsync(options.ViewPort),1125                SetUserAgentAsync(options.UserAgent));1126        }1127        /// <summary>1128        /// Takes a screenshot of the page1129        /// </summary>1130        /// <returns>The screenshot task.</returns>1131        /// <param name="file">The file path to save the image to. The screenshot type will be inferred from file extension.1132        /// If path is a relative path, then it is resolved relative to current working directory. If no path is provided,1133        /// the image won't be saved to the disk.</param>1134        public Task ScreenshotAsync(string file) => ScreenshotAsync(file, new ScreenshotOptions());1135        /// <summary>1136        /// Takes a screenshot of the page1137        /// </summary>1138        /// <returns>The screenshot task.</returns>1139        /// <param name="file">The file path to save the image to. The screenshot type will be inferred from file extension.1140        /// If path is a relative path, then it is resolved relative to current working directory. If no path is provided,1141        /// the image won't be saved to the disk.</param>1142        /// <param name="options">Screenshot options.</param>1143        public async Task ScreenshotAsync(string file, ScreenshotOptions options)1144        {1145            if (options == null)1146            {1147                throw new ArgumentNullException(nameof(options));1148            }1149            if (!options.Type.HasValue)1150            {1151                options.Type = ScreenshotOptions.GetScreenshotTypeFromFile(file);1152                if (options.Type == ScreenshotType.Jpeg && !options.Quality.HasValue)1153                {1154                    options.Quality = 90;1155                }1156            }1157            var data = await ScreenshotDataAsync(options).ConfigureAwait(false);1158            using (var fs = AsyncFileHelper.CreateStream(file, FileMode.Create))1159            {1160                await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false);1161            }1162        }1163        /// <summary>1164        /// Takes a screenshot of the page1165        /// </summary>1166        /// <returns>Task which resolves to a <see cref="Stream"/> containing the image data.</returns>1167        public Task<Stream> ScreenshotStreamAsync() => ScreenshotStreamAsync(new ScreenshotOptions());1168        /// <summary>1169        /// Takes a screenshot of the page1170        /// </summary>1171        /// <returns>Task which resolves to a <see cref="Stream"/> containing the image data.</returns>1172        /// <param name="options">Screenshot options.</param>1173        public async Task<Stream> ScreenshotStreamAsync(ScreenshotOptions options)1174            => new MemoryStream(await ScreenshotDataAsync(options).ConfigureAwait(false));1175        /// <summary>1176        /// Takes a screenshot of the page1177        /// </summary>1178        /// <returns>Task which resolves to a <see cref="string"/> containing the image data as base64.</returns>1179        public Task<string> ScreenshotBase64Async() => ScreenshotBase64Async(new ScreenshotOptions());1180        /// <summary>1181        /// Takes a screenshot of the page1182        /// </summary>1183        /// <returns>Task which resolves to a <see cref="string"/> containing the image data as base64.</returns>1184        /// <param name="options">Screenshot options.</param>1185        public Task<string> ScreenshotBase64Async(ScreenshotOptions options)1186        {1187            if (options == null)1188            {1189                throw new ArgumentNullException(nameof(options));1190            }1191            var screenshotType = options.Type;1192            if (!screenshotType.HasValue)1193            {1194                screenshotType = ScreenshotType.Png;1195            }1196            if (options.Quality.HasValue)1197            {1198                if (screenshotType != ScreenshotType.Jpeg)1199                {1200                    throw new ArgumentException($"options.Quality is unsupported for the {screenshotType} screenshots");1201                }1202                if (options.Quality < 0 || options.Quality > 100)1203                {1204                    throw new ArgumentException($"Expected options.quality to be between 0 and 100 (inclusive), got {options.Quality}");1205                }1206            }1207            if (options?.Clip?.Width == 0)1208            {1209                throw new PuppeteerException("Expected options.Clip.Width not to be 0.");1210            }1211            if (options?.Clip?.Height == 0)1212            {1213                throw new PuppeteerException("Expected options.Clip.Height not to be 0.");1214            }1215            if (options.Clip != null && options.FullPage)1216            {1217                throw new ArgumentException("options.clip and options.fullPage are exclusive");1218            }1219            return _screenshotTaskQueue.Enqueue(() => PerformScreenshot(screenshotType.Value, options));1220        }1221        /// <summary>1222        /// Takes a screenshot of the page1223        /// </summary>1224        /// <returns>Task which resolves to a <see cref="byte"/>[] containing the image data.</returns>1225        public Task<byte[]> ScreenshotDataAsync() => ScreenshotDataAsync(new ScreenshotOptions());1226        /// <summary>1227        /// Takes a screenshot of the page1228        /// </summary>1229        /// <returns>Task which resolves to a <see cref="byte"/>[] containing the image data.</returns>1230        /// <param name="options">Screenshot options.</param>1231        public async Task<byte[]> ScreenshotDataAsync(ScreenshotOptions options)1232            => Convert.FromBase64String(await ScreenshotBase64Async(options).ConfigureAwait(false));1233        /// <summary>1234        /// Returns page's title1235        /// </summary>1236        /// <returns>page's title</returns>1237        /// <see cref="Frame.GetTitleAsync"/>1238        public Task<string> GetTitleAsync() => MainFrame.GetTitleAsync();1239        /// <summary>1240        /// Closes the page.1241        /// </summary>1242        /// <param name="options">Close options.</param>1243        /// <returns>Task.</returns>1244        public Task CloseAsync(PageCloseOptions options = null)1245        {1246            if (!(Client?.Connection?.IsClosed ?? true))1247            {1248                var runBeforeUnload = options?.RunBeforeUnload ?? false;1249                if (runBeforeUnload)1250                {1251                    return Client.SendAsync("Page.close");1252                }1253                return Client.Connection.SendAsync("Target.closeTarget", new TargetCloseTargetRequest1254                {1255                    TargetId = Target.TargetId1256                }).ContinueWith(task => Target.CloseTask, TaskScheduler.Default);1257            }1258            _logger.LogWarning("Protocol error: Connection closed. Most likely the page has been closed.");1259            return _closeCompletedTcs.Task;1260        }1261        /// <summary>1262        /// Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled.1263        /// </summary>1264        /// <param name="enabled">sets the <c>enabled</c> state of the cache</param>1265        /// <returns>Task</returns>1266        public Task SetCacheEnabledAsync(bool enabled = true)1267            => FrameManager.NetworkManager.SetCacheEnabledAsync(enabled);1268        /// <summary>1269        /// Fetches an element with <paramref name="selector"/>, scrolls it into view if needed, and then uses <see cref="Mouse"/> to click in the center of the element.1270        /// </summary>1271        /// <param name="selector">A selector to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.</param>1272        /// <param name="options">click options</param>1273        /// <exception cref="SelectorException">If there's no element matching <paramref name="selector"/></exception>1274        /// <returns>Task which resolves when the element matching <paramref name="selector"/> is successfully clicked</returns>1275        public Task ClickAsync(string selector, ClickOptions options = null) => FrameManager.MainFrame.ClickAsync(selector, options);1276        /// <summary>1277        /// Fetches an element with <paramref name="selector"/>, scrolls it into view if needed, and then uses <see cref="Mouse"/> to hover over the center of the element.1278        /// </summary>1279        /// <param name="selector">A selector to search for element to hover. If there are multiple elements satisfying the selector, the first will be hovered.</param>1280        /// <exception cref="SelectorException">If there's no element matching <paramref name="selector"/></exception>1281        /// <returns>Task which resolves when the element matching <paramref name="selector"/> is successfully hovered</returns>1282        public Task HoverAsync(string selector) => FrameManager.MainFrame.HoverAsync(selector);1283        /// <summary>1284        /// Fetches an element with <paramref name="selector"/> and focuses it1285        /// </summary>1286        /// <param name="selector">A selector to search for element to focus. If there are multiple elements satisfying the selector, the first will be focused.</param>1287        /// <exception cref="SelectorException">If there's no element matching <paramref name="selector"/></exception>1288        /// <returns>Task which resolves when the element matching <paramref name="selector"/> is successfully focused</returns>1289        public Task FocusAsync(string selector) => FrameManager.MainFrame.FocusAsync(selector);1290        /// <summary>1291        /// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.1292        /// </summary>1293        /// <param name="selector">A selector of an element to type into. If there are multiple elements satisfying the selector, the first will be used.</param>1294        /// <param name="text">A text to type into a focused element</param>1295        /// <param name="options">The options to apply to the type operation.</param>1296        /// <exception cref="SelectorException">If there's no element matching <paramref name="selector"/></exception>1297        /// <remarks>1298        /// To press a special key, like <c>Control</c> or <c>ArrowDown</c> use <see cref="PuppeteerSharp.Input.Keyboard.PressAsync(string, PressOptions)"/>1299        /// </remarks>1300        /// <example>1301        /// <code>1302        /// await page.TypeAsync("#mytextarea", "Hello"); // Types instantly1303        /// await page.TypeAsync("#mytextarea", "World", new TypeOptions { Delay = 100 }); // Types slower, like a user1304        /// </code>1305        /// </example>1306        /// <returns>Task</returns>1307        public Task TypeAsync(string selector, string text, TypeOptions options = null)1308            => FrameManager.MainFrame.TypeAsync(selector, text, options);1309        /// <summary>1310        /// Executes a script in browser context1311        /// </summary>1312        /// <param name="script">Script to be evaluated in browser context</param>1313        /// <remarks>1314        /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.1315        /// </remarks>1316        /// <example>1317        /// An example of scraping information from all hyperlinks on the page.1318        /// <code>1319        /// var hyperlinkInfo = await page.EvaluateExpressionAsync(@"1320        ///     Array1321        ///        .from(document.querySelectorAll('a'))1322        ///        .map(n => ({1323        ///            text: n.innerText,1324        ///            href: n.getAttribute('href'),1325        ///            target: n.getAttribute('target')1326        ///         }))1327        /// ");1328        /// Console.WriteLine(hyperlinkInfo.ToString()); // Displays JSON array of hyperlinkInfo objects1329        /// </code>1330        /// </example>1331        /// <seealso href="https://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jtoken.htm"/>1332        /// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>1333        /// <returns>Task which resolves to script return value</returns>1334        public Task<JToken> EvaluateExpressionAsync(string script)1335            => FrameManager.MainFrame.EvaluateExpressionAsync<JToken>(script);1336        /// <summary>1337        /// Executes a script in browser context1338        /// </summary>1339        /// <typeparam name="T">The type to deserialize the result to</typeparam>1340        /// <param name="script">Script to be evaluated in browser context</param>1341        /// <remarks>1342        /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.1343        /// </remarks>1344        /// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>1345        /// <returns>Task which resolves to script return value</returns>1346        public Task<T> EvaluateExpressionAsync<T>(string script)1347            => FrameManager.MainFrame.EvaluateExpressionAsync<T>(script);1348        /// <summary>1349        /// Executes a function in browser context1350        /// </summary>1351        /// <param name="script">Script to be evaluated in browser context</param>1352        /// <param name="args">Arguments to pass to script</param>1353        /// <remarks>1354        /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.1355        /// <see cref="JSHandle"/> instances can be passed as arguments1356        /// </remarks>1357        /// <seealso cref="EvaluateExpressionAsync{T}(string)"/>1358        /// <returns>Task which resolves to script return value</returns>1359        public Task<JToken> EvaluateFunctionAsync(string script, params object[] args)1360            => FrameManager.MainFrame.EvaluateFunctionAsync<JToken>(script, args);1361        /// <summary>1362        /// Executes a function in browser context1363        /// </summary>1364        /// <typeparam name="T">The type to deserialize the result to</typeparam>1365        /// <param name="script">Script to be evaluated in browser context</param>1366        /// <param name="args">Arguments to pass to script</param>1367        /// <remarks>1368        /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.1369        /// <see cref="JSHandle"/> instances can be passed as arguments1370        /// </remarks>1371        /// <seealso cref="EvaluateExpressionAsync{T}(string)"/>1372        /// <returns>Task which resolves to script return value</returns>1373        public Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)1374            => FrameManager.MainFrame.EvaluateFunctionAsync<T>(script, args);1375        /// <summary>1376        /// Sets the user agent to be used in this page1377        /// </summary>1378        /// <param name="userAgent">Specific user agent to use in this page</param>1379        /// <returns>Task</returns>1380        public Task SetUserAgentAsync(string userAgent)1381            => FrameManager.NetworkManager.SetUserAgentAsync(userAgent);1382        /// <summary>1383        /// Sets extra HTTP headers that will be sent with every request the page initiates1384        /// </summary>1385        /// <param name="headers">Additional http headers to be sent with every request</param>1386        /// <returns>Task</returns>1387        public Task SetExtraHttpHeadersAsync(Dictionary<string, string> headers)1388        {1389            if (headers == null)1390            {1391                throw new ArgumentNullException(nameof(headers));1392            }1393            return FrameManager.NetworkManager.SetExtraHTTPHeadersAsync(headers);1394        }1395        /// <summary>1396        /// Provide credentials for http authentication <see href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication"/>1397        /// </summary>1398        /// <param name="credentials">The credentials</param>1399        /// <returns></returns>1400        /// <remarks>1401        /// To disable authentication, pass <c>null</c>1402        /// </remarks>1403        public Task AuthenticateAsync(Credentials credentials) => FrameManager.NetworkManager.AuthenticateAsync(credentials);1404        /// <summary>1405        /// Reloads the page1406        /// </summary>1407        /// <param name="options">Navigation options</param>1408        /// <returns>Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect</returns>1409        /// <seealso cref="ReloadAsync(int?, WaitUntilNavigation[])"/>1410        public async Task<Response> ReloadAsync(NavigationOptions options)1411        {1412            var navigationTask = WaitForNavigationAsync(options);1413            await Task.WhenAll(1414              navigationTask,1415              Client.SendAsync("Page.reload", new PageReloadRequest { FrameId = MainFrame.Id })).ConfigureAwait(false);1416            return navigationTask.Result;1417        }1418        /// <summary>1419        /// Reloads the page1420        /// </summary>1421        /// <param name="timeout">Maximum navigation time in milliseconds, defaults to 30 seconds, pass <c>0</c> to disable timeout. </param>1422        /// <param name="waitUntil">When to consider navigation succeeded, defaults to <see cref="WaitUntilNavigation.Load"/>. Given an array of <see cref="WaitUntilNavigation"/>, navigation is considered to be successful after all events have been fired</param>1423        /// <returns>Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect</returns>1424        /// <seealso cref="ReloadAsync(NavigationOptions)"/>1425        public Task<Response> ReloadAsync(int? timeout = null, WaitUntilNavigation[] waitUntil = null)1426            => ReloadAsync(new NavigationOptions { Timeout = timeout, WaitUntil = waitUntil });1427        /// <summary>1428        /// Triggers a change and input event once all the provided options have been selected.1429        /// If there's no <![CDATA[<select>]]> element matching selector, the method throws an error.1430        /// </summary>1431        /// <exception cref="SelectorException">If there's no element matching <paramref name="selector"/></exception>1432        /// <param name="selector">A selector to query page for</param>1433        /// <param name="values">Values of options to select. If the <![CDATA[<select>]]> has the multiple attribute,1434        /// all values are considered, otherwise only the first one is taken into account.</param>1435        /// <returns>Returns an array of option values that have been successfully selected.</returns>1436        /// <seealso cref="Frame.SelectAsync(string, string[])"/>1437        public Task<string[]> SelectAsync(string selector, params string[] values)1438            => MainFrame.SelectAsync(selector, values);1439        /// <summary>1440        /// Waits for a timeout1441        /// </summary>1442        /// <param name="milliseconds">The amount of time to wait.</param>1443        /// <returns>A task that resolves when after the timeout</returns>1444        /// <seealso cref="Frame.WaitForTimeoutAsync(int)"/>1445        public Task WaitForTimeoutAsync(int milliseconds)1446            => MainFrame.WaitForTimeoutAsync(milliseconds);1447        /// <summary>1448        /// Waits for a function to be evaluated to a truthy value1449        /// </summary>1450        /// <param name="script">Function to be evaluated in browser context</param>1451        /// <param name="options">Optional waiting parameters</param>1452        /// <param name="args">Arguments to pass to <c>script</c></param>1453        /// <returns>A task that resolves when the <c>script</c> returns a truthy value</returns>1454        /// <seealso cref="Frame.WaitForFunctionAsync(string, WaitForFunctionOptions, object[])"/>1455        public Task<JSHandle> WaitForFunctionAsync(string script, WaitForFunctionOptions options = null, params object[] args)1456            => MainFrame.WaitForFunctionAsync(script, options ?? new WaitForFunctionOptions(), args);1457        /// <summary>1458        /// Waits for a function to be evaluated to a truthy value1459        /// </summary>1460        /// <param name="script">Function to be evaluated in browser context</param>1461        /// <param name="args">Arguments to pass to <c>script</c></param>1462        /// <returns>A task that resolves when the <c>script</c> returns a truthy value</returns>1463        public Task<JSHandle> WaitForFunctionAsync(string script, params object[] args) => WaitForFunctionAsync(script, null, args);1464        /// <summary>1465        /// Waits for an expression to be evaluated to a truthy value1466        /// </summary>1467        /// <param name="script">Expression to be evaluated in browser context</param>1468        /// <param name="options">Optional waiting parameters</param>1469        /// <returns>A task that resolves when the <c>script</c> returns a truthy value</returns>1470        /// <seealso cref="Frame.WaitForExpressionAsync(string, WaitForFunctionOptions)"/>1471        public Task<JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptions options = null)1472            => MainFrame.WaitForExpressionAsync(script, options ?? new WaitForFunctionOptions());1473        /// <summary>1474        /// Waits for a selector to be added to the DOM1475        /// </summary>1476        /// <param name="selector">A selector of an element to wait for</param>1477        /// <param name="options">Optional waiting parameters</param>1478        /// <returns>A task that resolves when element specified by selector string is added to DOM.1479        /// Resolves to `null` if waiting for `hidden: true` and selector is not found in DOM.</returns>1480        /// <seealso cref="WaitForXPathAsync(string, WaitForSelectorOptions)"/>1481        /// <seealso cref="Frame.WaitForSelectorAsync(string, WaitForSelectorOptions)"/>1482        public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelectorOptions options = null)1483            => MainFrame.WaitForSelectorAsync(selector, options ?? new WaitForSelectorOptions());1484        /// <summary>1485        /// Waits for a xpath selector to be added to the DOM1486        /// </summary>1487        /// <param name="xpath">A xpath selector of an element to wait for</param>1488        /// <param name="options">Optional waiting parameters</param>1489        /// <returns>A task which resolves when element specified by xpath string is added to DOM.1490        /// Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM.</returns>1491        /// <example>1492        /// <code>1493        /// <![CDATA[1494        /// var browser = await Puppeteer.LaunchAsync(new LaunchOptions());1495        /// var page = await browser.NewPageAsync();1496        /// string currentURL = null;1497        /// page1498        ///     .WaitForXPathAsync("//img")1499        ///     .ContinueWith(_ => Console.WriteLine("First URL with image: " + currentURL));1500        /// foreach (var current in new[] { "https://example.com", "https://google.com", "https://bbc.com" })1501        /// {1502        ///     currentURL = current;1503        ///     await page.GoToAsync(currentURL);1504        /// }1505        /// await browser.CloseAsync();1506        /// ]]>1507        /// </code>1508        /// </example>1509        /// <seealso cref="WaitForSelectorAsync(string, WaitForSelectorOptions)"/>1510        /// <seealso cref="Frame.WaitForXPathAsync(string, WaitForSelectorOptions)"/>1511        public Task<ElementHandle> WaitForXPathAsync(string xpath, WaitForSelectorOptions options = null)1512            => MainFrame.WaitForXPathAsync(xpath, options ?? new WaitForSelectorOptions());1513        /// <summary>1514        /// This resolves when the page navigates to a new URL or reloads.1515        /// It is useful for when you run code which will indirectly cause the page to navigate.1516        /// </summary>1517        /// <param name="options">navigation options</param>1518        /// <returns>Task which resolves to the main resource response.1519        /// In case of multiple redirects, the navigation will resolve with the response of the last redirect.1520        /// In case of navigation to a different anchor or navigation due to History API usage, the navigation will resolve with `null`.1521        /// </returns>1522        /// <remarks>1523        /// Usage of the <c>History API</c> <see href="https://developer.mozilla.org/en-US/docs/Web/API/History_API"/> to change the URL is considered a navigation1524        /// </remarks>1525        /// <example>1526        /// <code>1527        /// <![CDATA[1528        /// var navigationTask = page.WaitForNavigationAsync();1529        /// await page.ClickAsync("a.my-link");1530        /// await navigationTask;1531        /// ]]>1532        /// </code>1533        /// </example>1534        public Task<Response> WaitForNavigationAsync(NavigationOptions options = null) => FrameManager.WaitForFrameNavigationAsync(FrameManager.MainFrame, options);1535        /// <summary>1536        /// Waits for Network Idle1537        /// </summary>1538        /// <param name="options">Optional waiting parameters</param>1539        /// <returns>returns Task which resolves when network is idle</returns>1540        /// <example>1541        /// <code>1542        /// <![CDATA[1543        /// page.EvaluateFunctionAsync("() => fetch('some-url')");1544        /// await page.WaitForNetworkIdle(); // The Task resolves after fetch above finishes1545        /// ]]>1546        /// </code>1547        /// </example>1548        public async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)1549        {1550            var timeout = options?.Timeout ?? DefaultTimeout;1551            var idleTime = options?.IdleTime ?? 500;1552            var networkIdleTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);1553            var idleTimer = new Timer1554            {1555                Interval = idleTime1556            };1557            idleTimer.Elapsed += (sender, args) =>1558            {1559                networkIdleTcs.TrySetResult(true);1560            };1561            var networkManager = FrameManager.NetworkManager;1562            void Evaluate()1563            {1564                idleTimer.Stop();1565                if (networkManager.NumRequestsInProgress == 0)1566                {1567                    idleTimer.Start();1568                }1569            }1570            void RequestEventListener(object sender, RequestEventArgs e) => Evaluate();1571            void ResponseEventListener(object sender, ResponseCreatedEventArgs e) => Evaluate();1572            void Cleanup()1573            {1574                idleTimer.Stop();1575                idleTimer.Dispose();1576                networkManager.Request -= RequestEventListener;1577                networkManager.Response -= ResponseEventListener;1578            }1579            networkManager.Request += RequestEventListener;1580            networkManager.Response += ResponseEventListener;1581            Evaluate();1582            await Task.WhenAny(networkIdleTcs.Task, SessionClosedTask).WithTimeout(timeout, t =>1583            {1584                Cleanup();1585                return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");1586            }).ConfigureAwait(false);1587            Cleanup();1588            if (SessionClosedTask.IsFaulted)1589            {1590                await SessionClosedTask.ConfigureAwait(false);1591            }1592        }1593        /// <summary>1594        /// Waits for a request.1595        /// </summary>1596        /// <example>1597        /// <code>1598        /// <![CDATA[1599        /// var firstRequest = await page.WaitForRequestAsync("http://example.com/resource");1600        /// return firstRequest.Url;1601        /// ]]>1602        /// </code>1603        /// </example>1604        /// <returns>A task which resolves when a matching request was made.</returns>1605        /// <param name="url">URL to wait for.</param>1606        /// <param name="options">Options.</param>1607        public Task<Request> WaitForRequestAsync(string url, WaitForOptions options = null)1608            => WaitForRequestAsync(request => request.Url == url, options);1609        /// <summary>1610        /// Waits for a request.1611        /// </summary>1612        /// <example>1613        /// <code>1614        /// <![CDATA[1615        /// var request = await page.WaitForRequestAsync(request => request.Url === "http://example.com" && request.Method === HttpMethod.Get;1616        /// return request.Url;1617        /// ]]>1618        /// </code>1619        /// </example>1620        /// <returns>A task which resolves when a matching request was made.</returns>1621        /// <param name="predicate">Function which looks for a matching request.</param>1622        /// <param name="options">Options.</param>1623        public async Task<Request> WaitForRequestAsync(Func<Request, bool> predicate, WaitForOptions options = null)1624        {1625            var timeout = options?.Timeout ?? DefaultTimeout;1626            var requestTcs = new TaskCompletionSource<Request>(TaskCreationOptions.RunContinuationsAsynchronously);1627            void requestEventListener(object sender, RequestEventArgs e)1628            {1629                if (predicate(e.Request))1630                {1631                    requestTcs.TrySetResult(e.Request);1632                    FrameManager.NetworkManager.Request -= requestEventListener;1633                }1634            }1635            FrameManager.NetworkManager.Request += requestEventListener;1636            await Task.WhenAny(requestTcs.Task, SessionClosedTask).WithTimeout(timeout, t =>1637            {1638                FrameManager.NetworkManager.Request -= requestEventListener;1639                return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");1640            }).ConfigureAwait(false);1641            if (SessionClosedTask.IsFaulted)1642            {1643                await SessionClosedTask.ConfigureAwait(false);1644            }1645            return await requestTcs.Task.ConfigureAwait(false);1646        }1647        /// <summary>1648        /// Waits for a response.1649        /// </summary>1650        /// <example>1651        /// <code>1652        /// <![CDATA[1653        /// var firstResponse = await page.WaitForResponseAsync("http://example.com/resource");1654        /// return firstResponse.Url;1655        /// ]]>1656        /// </code>1657        /// </example>1658        /// <returns>A task which resolves when a matching response is received.</returns>1659        /// <param name="url">URL to wait for.</param>1660        /// <param name="options">Options.</param>1661        public Task<Response> WaitForResponseAsync(string url, WaitForOptions options = null)1662            => WaitForResponseAsync(response => response.Url == url, options);1663        /// <summary>1664        /// Waits for a response.1665        /// </summary>1666        /// <example>1667        /// <code>1668        /// <![CDATA[1669        /// var response = await page.WaitForResponseAsync(response => response.Url === "http://example.com" && response.Status === HttpStatus.Ok;1670        /// return response.Url;1671        /// ]]>1672        /// </code>1673        /// </example>1674        /// <returns>A task which resolves when a matching response is received.</returns>1675        /// <param name="predicate">Function which looks for a matching response.</param>1676        /// <param name="options">Options.</param>1677        public async Task<Response> WaitForResponseAsync(Func<Response, bool> predicate, WaitForOptions options = null)1678        {1679            var timeout = options?.Timeout ?? DefaultTimeout;1680            var responseTcs = new TaskCompletionSource<Response>(TaskCreationOptions.RunContinuationsAsynchronously);1681            void responseEventListener(object sender, ResponseCreatedEventArgs e)1682            {1683                if (predicate(e.Response))1684                {1685                    responseTcs.TrySetResult(e.Response);1686                    FrameManager.NetworkManager.Response -= responseEventListener;1687                }1688            }1689            FrameManager.NetworkManager.Response += responseEventListener;1690            await Task.WhenAny(responseTcs.Task, SessionClosedTask).WithTimeout(timeout).ConfigureAwait(false);1691            if (SessionClosedTask.IsFaulted)1692            {1693                await SessionClosedTask.ConfigureAwait(false);1694            }1695            return await responseTcs.Task.ConfigureAwait(false);1696        }1697        /// <summary>1698        /// Waits for a page to open a file picker1699        /// </summary>1700        /// <remarks>1701        /// In non-headless Chromium, this method results in the native file picker dialog **not showing up** for the user.1702        /// </remarks>1703        /// <example>1704        /// This method is typically coupled with an action that triggers file choosing.1705        /// The following example clicks a button that issues a file chooser, and then1706        /// responds with `/tmp/myfile.pdf` as if a user has selected this file.1707        /// <code>1708        /// <![CDATA[1709        /// var waitTask = page.WaitForFileChooserAsync();1710        /// await Task.WhenAll(1711        ///     waitTask,1712        ///     page.ClickAsync("#upload-file-button")); // some button that triggers file selection1713        ///1714        /// await waitTask.Result.AcceptAsync('/tmp/myfile.pdf');1715        /// ]]>1716        /// </code>1717        ///1718        /// This must be called *before* the file chooser is launched. It will not return a currently active file chooser.1719        /// </example>1720        /// <param name="options">Optional waiting parameters.</param>1721        /// <returns>A task that resolves after a page requests a file picker.</returns>1722        public async Task<FileChooser> WaitForFileChooserAsync(WaitForFileChooserOptions options = null)1723        {1724            if (!_fileChooserInterceptors.Any())1725            {1726                await Client.SendAsync("Page.setInterceptFileChooserDialog", new PageSetInterceptFileChooserDialog1727                {1728                    Enabled = true1729                }).ConfigureAwait(false);1730            }1731            var timeout = options?.Timeout ?? _timeoutSettings.Timeout;1732            var tcs = new TaskCompletionSource<FileChooser>(TaskCreationOptions.RunContinuationsAsynchronously);1733            var guid = Guid.NewGuid();1734            _fileChooserInterceptors.TryAdd(guid, tcs);1735            try1736            {1737                return await tcs.Task.WithTimeout(timeout).ConfigureAwait(false);1738            }1739            catch (Exception)1740            {1741                _fileChooserInterceptors.TryRemove(guid, out _);1742                throw;1743            }1744        }1745        /// <summary>1746        /// Navigate to the previous page in history.1747        /// </summary>1748        /// <returns>Task that resolves to the main resource response. In case of multiple redirects,1749        /// the navigation will resolve with the response of the last redirect. If can not go back, resolves to null.</returns>1750        /// <param name="options">Navigation parameters.</param>1751        public Task<Response> GoBackAsync(NavigationOptions options = null) => GoAsync(-1, options);1752        /// <summary>1753        /// Navigate to the next page in history.1754        /// </summary>1755        /// <returns>Task that resolves to the main resource response. In case of multiple redirects,1756        /// the navigation will resolve with the response of the last redirect. If can not go forward, resolves to null.</returns>1757        /// <param name="options">Navigation parameters.</param>1758        public Task<Response> GoForwardAsync(NavigationOptions options = null) => GoAsync(1, options);1759        /// <summary>1760        /// Resets the background color and Viewport after taking Screenshots using BurstMode.1761        /// </summary>1762        /// <returns>The burst mode off.</returns>1763        public Task SetBurstModeOffAsync()1764        {1765            _screenshotBurstModeOn = false;1766            if (_screenshotBurstModeOptions != null)1767            {1768                ResetBackgroundColorAndViewportAsync(_screenshotBurstModeOptions);1769            }1770            return Task.CompletedTask;1771        }1772        /// <summary>1773        /// Brings page to front (activates tab).1774        /// </summary>1775        /// <returns>A task that resolves when the message has been sent to Chromium.</returns>1776        public Task BringToFrontAsync() => Client.SendAsync("Page.bringToFront");1777        /// <summary>1778        /// Simulates the given vision deficiency on the page.1779        /// </summary>1780        /// <example>1781        /// await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Achromatopsia);1782        /// await Page.ScreenshotAsync("Achromatopsia.png");1783        /// </example>1784        /// <param name="type">The type of deficiency to simulate, or <see cref="VisionDeficiency.None"/> to reset.</param>1785        /// <returns>A task that resolves when the message has been sent to the browser.</returns>1786        public Task EmulateVisionDeficiencyAsync(VisionDeficiency type)1787            => Client.SendAsync("Emulation.setEmulatedVisionDeficiency", new EmulationSetEmulatedVisionDeficiencyRequest1788            {1789                Type = type,1790            });1791        /// <summary>1792        /// Changes the timezone of the page.1793        /// </summary>1794        /// <param name="timezoneId">Timezone to set. See <seealso href="https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1" >ICU’s `metaZones.txt`</seealso>1795        /// for a list of supported timezone IDs. Passing `null` disables timezone emulation.</param>1796        /// <returns>The viewport task.</returns>1797        public async Task EmulateTimezoneAsync(string timezoneId)1798        {1799            try1800            {1801                await Client.SendAsync("Emulation.setTimezoneOverride", new EmulateTimezoneRequest1802                {1803                    TimezoneId = timezoneId ?? string.Empty1804                }).ConfigureAwait(false);1805            }1806            catch (Exception ex) when (ex.Message.Contains("Invalid timezone"))1807            {1808                throw new PuppeteerException($"Invalid timezone ID: {timezoneId}");1809            }1810        }1811        /// <summary>1812        /// Emulates the idle state.1813        /// If no arguments set, clears idle state emulation.1814        /// </summary>1815        /// <example>1816        /// <code>1817        /// // set idle emulation1818        /// await page.EmulateIdleStateAsync(new EmulateIdleOverrides() {IsUserActive = true, IsScreenUnlocked = false});1819        /// // do some checks here1820        /// ...1821        /// // clear idle emulation1822        /// await page.EmulateIdleStateAsync();1823        /// </code>1824        /// </example>1825        /// <param name="overrides">Overrides</param>1826        /// <returns>A task that resolves when the message has been sent to the browser.</returns>1827        public async Task EmulateIdleStateAsync(EmulateIdleOverrides overrides = null)1828        {1829            if (overrides != null)1830            {1831                await Client.SendAsync(1832                    "Emulation.setIdleOverride",1833                    new EmulationSetIdleOverrideRequest1834                    {1835                        IsUserActive = overrides.IsUserActive,1836                        IsScreenUnlocked = overrides.IsScreenUnlocked,1837                    }).ConfigureAwait(false);1838            }1839            else1840            {1841                await Client.SendAsync("Emulation.clearIdleOverride").ConfigureAwait(false);1842            }1843        }1844        /// <summary>1845        /// Enables CPU throttling to emulate slow CPUs.1846        /// </summary>1847        /// <param name="factor">Throttling rate as a slowdown factor (1 is no throttle, 2 is 2x slowdown, etc).</param>1848        /// <returns>A task that resolves when the message has been sent to the browser.</returns>1849        public Task EmulateCPUThrottlingAsync(decimal? factor = null)1850        {1851            if (factor != null && factor < 1)1852            {1853                throw new ArgumentException("Throttling rate should be greater or equal to 1", nameof(factor));1854            }1855            return Client.SendAsync("Emulation.setCPUThrottlingRate", new EmulationSetCPUThrottlingRateRequest1856            {1857                Rate = factor ?? 11858            });1859        }1860        internal void OnPopup(Page popupPage) => Popup?.Invoke(this, new PopupEventArgs { PopupPage = popupPage });1861        internal static async Task<Page> CreateAsync(1862            CDPSession client,1863            Target target,1864            bool ignoreHTTPSErrors,1865            ViewPortOptions defaultViewPort,1866            TaskQueue screenshotTaskQueue)1867        {1868            var page = new Page(client, target, screenshotTaskQueue);1869            await page.InitializeAsync(ignoreHTTPSErrors).ConfigureAwait(false);1870            if (defaultViewPort != null)1871            {1872                await page.SetViewportAsync(defaultViewPort).ConfigureAwait(false);1873            }1874            return page;1875        }1876        private async Task InitializeAsync(bool ignoreHTTPSErrors)1877        {1878            FrameManager = await FrameManager.CreateFrameManagerAsync(Client, this, ignoreHTTPSErrors, _timeoutSettings).ConfigureAwait(false);1879            var networkManager = FrameManager.NetworkManager;1880            Client.MessageReceived += Client_MessageReceived;1881            FrameManager.FrameAttached += (_, e) => FrameAttached?.Invoke(this, e);1882            FrameManager.FrameDetached += (_, e) => FrameDetached?.Invoke(this, e);1883            FrameManager.FrameNavigated += (_, e) => FrameNavigated?.Invoke(this, e);1884            networkManager.Request += (_, e) => Request?.Invoke(this, e);1885            networkManager.RequestFailed += (_, e) => RequestFailed?.Invoke(this, e);1886            networkManager.Response += (_, e) => Response?.Invoke(this, e);1887            networkManager.RequestFinished += (_, e) => RequestFinished?.Invoke(this, e);1888            networkManager.RequestServedFromCache += (_, e) => RequestServedFromCache?.Invoke(this, e);1889            await Task.WhenAll(1890               Client.SendAsync("Target.setAutoAttach", new TargetSetAutoAttachRequest1891               {1892                   AutoAttach = true,1893                   WaitForDebuggerOnStart = false,1894                   Flatten = true1895               }),1896               Client.SendAsync("Performance.enable", null),1897               Client.SendAsync("Log.enable", null)).ConfigureAwait(false);1898        }1899        private async Task<Response> GoAsync(int delta, NavigationOptions options)1900        {1901            var history = await Client.SendAsync<PageGetNavigationHistoryResponse>("Page.getNavigationHistory").ConfigureAwait(false);1902            if (history.Entries.Count <= history.CurrentIndex + delta || history.CurrentIndex + delta < 0)1903            {1904                return null;1905            }1906            var entry = history.Entries[history.CurrentIndex + delta];1907            var waitTask = WaitForNavigationAsync(options);1908            await Task.WhenAll(1909                waitTask,1910                Client.SendAsync("Page.navigateToHistoryEntry", new PageNavigateToHistoryEntryRequest1911                {1912                    EntryId = entry.Id1913                })).ConfigureAwait(false);1914            return waitTask.Result;1915        }1916        private Dictionary<string, decimal> BuildMetricsObject(List<Metric> metrics)1917        {1918            var result = new Dictionary<string, decimal>();1919            foreach (var item in metrics)1920            {1921                if (SupportedMetrics.Contains(item.Name))1922                {1923                    result.Add(item.Name, item.Value);1924                }1925            }1926            return result;1927        }1928        private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOptions options)1929        {1930            if (!_screenshotBurstModeOn)1931            {1932                await Client.SendAsync("Target.activateTarget", new TargetActivateTargetRequest1933                {1934                    TargetId = Target.TargetId1935                }).ConfigureAwait(false);1936            }1937            var clip = options.Clip != null ? ProcessClip(options.Clip) : null;1938            if (!_screenshotBurstModeOn)1939            {1940                if (options != null && options.FullPage)1941                {1942                    var metrics = _screenshotBurstModeOn1943                        ? _burstModeMetrics :1944                        await Client.SendAsync<PageGetLayoutMetricsResponse>("Page.getLayoutMetrics").ConfigureAwait(false);1945                    if (options.BurstMode)1946                    {1947                        _burstModeMetrics = metrics;1948                    }1949                    var contentSize = metrics.ContentSize;1950                    var width = Convert.ToInt32(Math.Ceiling(contentSize.Width));1951                    var height = Convert.ToInt32(Math.Ceiling(contentSize.Height));1952                    // Overwrite clip for full page at all times.1953                    clip = new Clip1954                    {1955                        X = 0,1956                        Y = 0,1957                        Width = width,1958                        Height = height,1959                        Scale = 11960                    };1961                    var isMobile = Viewport?.IsMobile ?? false;1962                    var deviceScaleFactor = Viewport?.DeviceScaleFactor ?? 1;1963                    var isLandscape = Viewport?.IsLandscape ?? false;1964                    var screenOrientation = isLandscape ?1965                        new ScreenOrientation1966                        {1967                            Angle = 90,1968                            Type = ScreenOrientationType.LandscapePrimary1969                        } :1970                        new ScreenOrientation1971                        {1972                            Angle = 0,1973                            Type = ScreenOrientationType.PortraitPrimary1974                        };1975                    await Client.SendAsync("Emulation.setDeviceMetricsOverride", new EmulationSetDeviceMetricsOverrideRequest1976                    {1977                        Mobile = isMobile,1978                        Width = width,1979                        Height = height,1980                        DeviceScaleFactor = deviceScaleFactor,1981                        ScreenOrientation = screenOrientation1982                    }).ConfigureAwait(false);1983                }1984                if (options?.OmitBackground == true && type == ScreenshotType.Png)1985                {1986                    await SetTransparentBackgroundColorAsync().ConfigureAwait(false);1987                }1988            }1989            var screenMessage = new PageCaptureScreenshotRequest1990            {1991                Format = type.ToString().ToLower(CultureInfo.CurrentCulture)1992            };1993            if (options.Quality.HasValue)1994            {1995                screenMessage.Quality = options.Quality.Value;1996            }1997            if (clip != null)1998            {1999                screenMessage.Clip = clip;2000            }2001            var result = await Client.SendAsync<PageCaptureScreenshotResponse>("Page.captureScreenshot", screenMessage).ConfigureAwait(false);2002            if (options.BurstMode)2003            {2004                _screenshotBurstModeOptions = options;2005                _screenshotBurstModeOn = true;2006            }2007            else2008            {2009                await ResetBackgroundColorAndViewportAsync(options).ConfigureAwait(false);2010            }2011            return result.Data;2012        }2013        private Clip ProcessClip(Clip clip)2014        {2015            var x = Math.Round(clip.X);2016            var y = Math.Round(clip.Y);2017            return new Clip2018            {2019                X = x,2020                Y = y,2021                Width = Math.Round(clip.Width + clip.X - x, MidpointRounding.AwayFromZero),2022                Height = Math.Round(clip.Height + clip.Y - y, MidpointRounding.AwayFromZero),2023                Scale = clip.Scale2024            };2025        }2026        private Task ResetBackgroundColorAndViewportAsync(ScreenshotOptions options)2027        {2028            var omitBackgroundTask = options?.OmitBackground == true && options.Type == ScreenshotType.Png ?2029                ResetDefaultBackgroundColorAsync() : Task.CompletedTask;2030            var setViewPortTask = (options?.FullPage == true && Viewport != null) ?2031                SetViewportAsync(Viewport) : Task.CompletedTask;2032            return Task.WhenAll(omitBackgroundTask, setViewPortTask);2033        }2034        private Task ResetDefaultBackgroundColorAsync()2035            => Client.SendAsync("Emulation.setDefaultBackgroundColorOverride");2036        private Task SetTransparentBackgroundColorAsync()2037            => Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new EmulationSetDefaultBackgroundColorOverrideRequest2038            {2039                Color = new EmulationSetDefaultBackgroundColorOverrideColor2040                {2041                    R = 0,2042                    G = 0,2043                    B = 0,2044                    A = 02045                }2046            });2047        private decimal ConvertPrintParameterToInches(object parameter)2048        {2049            if (parameter == null)2050            {2051                return 0;2052            }2053            decimal pixels;2054            if (parameter is decimal || parameter is int)2055            {2056                pixels = Convert.ToDecimal(parameter, CultureInfo.CurrentCulture);2057            }2058            else2059            {2060                var text = parameter.ToString();2061                var unit = text.Substring(text.Length - 2).ToLower(CultureInfo.CurrentCulture);2062                string valueText;2063                if (_unitToPixels.ContainsKey(unit))2064                {2065                    valueText = text.Substring(0, text.Length - 2);2066                }2067                else2068                {2069                    // In case of unknown unit try to parse the whole parameter as number of pixels.2070                    // This is consistent with phantom's paperSize behavior.2071                    unit = "px";2072                    valueText = text;2073                }2074                if (decimal.TryParse(valueText, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var number))2075                {2076                    pixels = number * _unitToPixels[unit];2077                }2078                else2079                {2080                    throw new ArgumentException($"Failed to parse parameter value: '{text}'", nameof(parameter));2081                }2082            }2083            return pixels / 96;2084        }2085        private async void Client_MessageReceived(object sender, MessageEventArgs e)2086        {2087            try2088            {2089                switch (e.MessageID)2090                {2091                    case "Page.domContentEventFired":2092                        DOMContentLoaded?.Invoke(this, EventArgs.Empty);2093                        break;2094                    case "Page.loadEventFired":2095                        Load?.Invoke(this, EventArgs.Empty);2096                        break;2097                    case "Runtime.consoleAPICalled":2098                        await OnConsoleAPIAsync(e.MessageData.ToObject<PageConsoleResponse>(true)).ConfigureAwait(false);2099                        break;2100                    case "Page.javascriptDialogOpening":2101                        OnDialog(e.MessageData.ToObject<PageJavascriptDialogOpeningResponse>(true));2102                        break;2103                    case "Runtime.exceptionThrown":2104                        HandleException(e.MessageData.ToObject<RuntimeExceptionThrownResponse>(true).ExceptionDetails);2105                        break;2106                    case "Inspector.targetCrashed":2107                        OnTargetCrashed();2108                        break;2109                    case "Performance.metrics":2110                        EmitMetrics(e.MessageData.ToObject<PerformanceMetricsResponse>(true));2111                        break;2112                    case "Target.attachedToTarget":2113                        await OnAttachedToTargetAsync(e.MessageData.ToObject<TargetAttachedToTargetResponse>(true)).ConfigureAwait(false);2114                        break;2115                    case "Target.detachedFromTarget":2116                        OnDetachedFromTarget(e.MessageData.ToObject<TargetDetachedFromTargetResponse>(true));2117                        break;2118                    case "Log.entryAdded":2119                        await OnLogEntryAddedAsync(e.MessageData.ToObject<LogEntryAddedResponse>(true)).ConfigureAwait(false);2120                        break;2121                    case "Runtime.bindingCalled":2122                        await OnBindingCalled(e.MessageData.ToObject<BindingCalledResponse>(true)).ConfigureAwait(false);2123                        break;2124                    case "Page.fileChooserOpened":2125                        await OnFileChooserAsync(e.MessageData.ToObject<PageFileChooserOpenedResponse>(true)).ConfigureAwait(false);2126                        break;2127                }2128            }2129            catch (Exception ex)2130            {2131                var message = $"Page failed to process {e.MessageID}. {ex.Message}. {ex.StackTrace}";2132                _logger.LogError(ex, message);2133                Client.Close(message);2134            }2135        }2136        private async Task OnFileChooserAsync(PageFileChooserOpenedResponse e)2137        {2138            if (_fileChooserInterceptors.Count == 0)2139            {2140                try2141                {2142                    await Client.SendAsync("Page.handleFileChooser", new PageHandleFileChooserRequest2143                    {2144                        Action = FileChooserAction.Fallback2145                    }).ConfigureAwait(false);2146                    return;2147                }2148                catch (Exception ex)2149                {2150                    _logger.LogError(ex, ex.ToString());2151                }2152            }2153            var frame = await FrameManager.GetFrameAsync(e.FrameId).ConfigureAwait(false);2154            var context = await frame.GetExecutionContextAsync().ConfigureAwait(false);2155            var element = await context.AdoptBackendNodeAsync(e.BackendNodeId).ConfigureAwait(false);2156            var fileChooser = new FileChooser(element, e);2157            while (_fileChooserInterceptors.Count > 0)2158            {2159                var key = _fileChooserInterceptors.FirstOrDefault().Key;2160                if (_fileChooserInterceptors.TryRemove(key, out var tcs))2161                {2162                    tcs.TrySetResult(fileChooser);2163                }2164            }2165        }2166        private async Task OnBindingCalled(BindingCalledResponse e)2167        {2168            string expression;2169            try2170            {2171                var result = await ExecuteBinding(e).ConfigureAwait(false);2172                expression = EvaluationString(2173                    @"function deliverResult(name, seq, result) {2174                        window[name]['callbacks'].get(seq).resolve(result);2175                        window[name]['callbacks'].delete(seq);2176                    }",2177                    e.BindingPayload.Name,2178                    e.BindingPayload.Seq,2179                    result);2180            }2181            catch (Exception ex)2182            {2183                if (ex is TargetInvocationException)2184                {2185                    ex = ex.InnerException;2186                }2187                expression = EvaluationString(2188                    @"function deliverError(name, seq, message, stack) {2189                        const error = new Error(message);2190                        error.stack = stack;2191                        window[name]['callbacks'].get(seq).reject(error);2192                        window[name]['callbacks'].delete(seq);2193                    }",2194                    e.BindingPayload.Name,2195                    e.BindingPayload.Seq,2196                    ex.Message,2197                    ex.StackTrace);2198            }2199            Client.Send("Runtime.evaluate", new2200            {2201                expression,2202                contextId = e.ExecutionContextId2203            });2204        }2205        private async Task<object> ExecuteBinding(BindingCalledResponse e)2206        {2207            const string taskResultPropertyName = "Result";2208            object result;2209            var binding = _pageBindings[e.BindingPayload.Name];2210            var methodParams = binding.Method.GetParameters().Select(parameter => parameter.ParameterType).ToArray();2211            var args = e.BindingPayload.Args.Select((token, i) => token.ToObject(methodParams[i])).ToArray();2212            result = binding.DynamicInvoke(args);2213            if (result is Task taskResult)2214            {2215                await taskResult.ConfigureAwait(false);2216                if (taskResult.GetType().IsGenericType)2217                {2218                    // the task is already awaited and therefore the call to property Result will not deadlock2219                    result = taskResult.GetType().GetProperty(taskResultPropertyName).GetValue(taskResult);2220                }2221            }2222            return result;2223        }2224        private void OnDetachedFromTarget(TargetDetachedFromTargetResponse e)2225        {2226            var sessionId = e.SessionId;2227            if (_workers.TryGetValue(sessionId, out var worker))2228            {2229                WorkerDestroyed?.Invoke(this, new WorkerEventArgs(worker));2230                _workers.Remove(sessionId);2231            }2232        }2233        private async Task OnAttachedToTargetAsync(TargetAttachedToTargetResponse e)2234        {2235            var targetInfo = e.TargetInfo;2236            var sessionId = e.SessionId;2237            if (targetInfo.Type != TargetType.Worker && targetInfo.Type != TargetType.iFrame)2238            {2239                try2240                {2241                    await Client.SendAsync("Target.detachFromTarget", new TargetDetachFromTargetRequest2242                    {2243                        SessionId = sessionId2244                    }).ConfigureAwait(false);2245                }2246                catch (Exception ex)2247                {2248                    _logger.LogError(ex.ToString());2249                }2250                return;2251            }2252            var session = Connection.FromSession(Client).GetSession(sessionId);2253            var worker = new Worker(session, targetInfo.Url, AddConsoleMessageAsync, HandleException);2254            _workers[sessionId] = worker;2255            WorkerCreated?.Invoke(this, new WorkerEventArgs(worker));2256        }2257        private async Task OnLogEntryAddedAsync(LogEntryAddedResponse e)2258        {2259            if (e.Entry.Args != null)2260            {2261                foreach (var arg in e.Entry?.Args)2262                {2263                    await RemoteObjectHelper.ReleaseObjectAsync(Client, arg, _logger).ConfigureAwait(false);2264                }2265            }2266            if (e.Entry.Source != TargetType.Worker)2267            {2268                Console?.Invoke(this, new ConsoleEventArgs(new ConsoleMessage(2269                    e.Entry.Level,2270                    e.Entry.Text,2271                    null,2272                    new ConsoleMessageLocation2273                    {2274                        URL = e.Entry.URL,2275                        LineNumber = e.Entry.LineNumber2276                    })));2277            }2278        }2279        private void OnTargetCrashed()2280        {2281            if (Error == null)2282            {2283                throw new TargetCrashedException();2284            }2285            Error.Invoke(this, new ErrorEventArgs("Page crashed!"));2286        }2287        private void EmitMetrics(PerformanceMetricsResponse metrics)2288            => Metrics?.Invoke(this, new MetricEventArgs(metrics.Title, BuildMetricsObject(metrics.Metrics)));2289        private void HandleException(EvaluateExceptionResponseDetails exceptionDetails)2290            => PageError?.Invoke(this, new PageErrorEventArgs(GetExceptionMessage(exceptionDetails)));2291        private string GetExceptionMessage(EvaluateExceptionResponseDetails exceptionDetails)2292        {2293            if (exceptionDetails.Exception != null)2294            {2295                return exceptionDetails.Exception.Description;2296            }2297            var message = exceptionDetails.Text;2298            if (exceptionDetails.StackTrace != null)2299            {2300                foreach (var callframe in exceptionDetails.StackTrace.CallFrames)2301                {2302                    var location = $"{callframe.Url}:{callframe.LineNumber}:{callframe.ColumnNumber}";2303                    var functionName = callframe.FunctionName ?? "<anonymous>";2304                    message += $"\n at {functionName} ({location})";2305                }2306            }2307            return message;2308        }2309        private void OnDialog(PageJavascriptDialogOpeningResponse message)2310        {2311            var dialog = new Dialog(Client, message.Type, message.Message, message.DefaultPrompt);2312            Dialog?.Invoke(this, new DialogEventArgs(dialog));2313        }2314        private Task OnConsoleAPIAsync(PageConsoleResponse message)2315        {2316            if (message.ExecutionContextId == 0)2317            {2318                return Task.CompletedTask;2319            }2320            var ctx = FrameManager.ExecutionContextById(message.ExecutionContextId);2321            var values = message.Args.Select(ctx.CreateJSHandle).ToArray();2322            return AddConsoleMessageAsync(message.Type, values, message.StackTrace);2323        }2324        private async Task AddConsoleMessageAsync(ConsoleType type, JSHandle[] values, Messaging.StackTrace stackTrace)2325        {2326            if (Console?.GetInvocationList().Length == 0)2327            {2328                await Task.WhenAll(values.Select(v => RemoteObjectHelper.ReleaseObjectAsync(Client, v.RemoteObject, _logger))).ConfigureAwait(false);2329                return;2330            }2331            var tokens = values.Select(i => i.RemoteObject.ObjectId != null2332                ? i.ToString()2333                : RemoteObjectHelper.ValueFromRemoteObject<string>(i.RemoteObject));2334            var location = new ConsoleMessageLocation();2335            if (stackTrace?.CallFrames?.Length > 0)2336            {2337                var callFrame = stackTrace.CallFrames[0];2338                location.URL = callFrame.URL;2339                location.LineNumber = callFrame.LineNumber;2340                location.ColumnNumber = callFrame.ColumnNumber;2341            }2342            var consoleMessage = new ConsoleMessage(type, string.Join(" ", tokens), values, location);2343            Console?.Invoke(this, new ConsoleEventArgs(consoleMessage));2344        }2345        private async Task ExposeFunctionAsync(string name, Delegate puppeteerFunction)2346        {2347            if (_pageBindings.ContainsKey(name))2348            {2349                throw new PuppeteerException($"Failed to add page binding with name {name}: window['{name}'] already exists!");2350            }2351            _pageBindings.Add(name, puppeteerFunction);2352            const string addPageBinding = @"function addPageBinding(bindingName) {2353              const binding = window[bindingName];2354              window[bindingName] = (...args) => {2355                const me = window[bindingName];2356                let callbacks = me['callbacks'];2357                if (!callbacks) {2358                  callbacks = new Map();2359                  me['callbacks'] = callbacks;2360                }2361                const seq = (me['lastSeq'] || 0) + 1;2362                me['lastSeq'] = seq;2363                const promise = new Promise((resolve, reject) => callbacks.set(seq, {resolve, reject}));2364                binding(JSON.stringify({name: bindingName, seq, args}));2365                return promise;2366              };2367            }";2368            var expression = EvaluationString(addPageBinding, name);2369            await Client.SendAsync("Runtime.addBinding", new RuntimeAddBindingRequest { Name = name }).ConfigureAwait(false);2370            await Client.SendAsync("Page.addScriptToEvaluateOnNewDocument", new PageAddScriptToEvaluateOnNewDocumentRequest2371            {2372                Source = expression2373            }).ConfigureAwait(false);2374            await Task.WhenAll(Frames.Select(2375                frame => frame2376                    .EvaluateExpressionAsync(expression)2377                    .ContinueWith(2378                        task =>2379                        {2380                            if (task.IsFaulted)2381                            {2382                                _logger.LogError(task.Exception.ToString());2383                            }2384                        },...

Full Screen

Full Screen

FrameManager.cs

Source:FrameManager.cs Github

copy

Full Screen

...337            {338                return;339            }340            _isolatedWorlds.Add(name);341            await Client.SendAsync("Page.addScriptToEvaluateOnNewDocument", new PageAddScriptToEvaluateOnNewDocumentRequest342            {343                Source = $"//# sourceURL={ExecutionContext.EvaluationScriptUrl}",344                WorldName = name,345            }).ConfigureAwait(false);346            try347            {348                await Task.WhenAll(GetFrames().Select(frame => Client.SendAsync("Page.createIsolatedWorld", new PageCreateIsolatedWorldRequest349                {350                    FrameId = frame.Id,351                    GrantUniveralAccess = true,352                    WorldName = name353                }))).ConfigureAwait(false);354            }355            catch (PuppeteerException)...

Full Screen

Full Screen

PageAddScriptToEvaluateOnNewDocumentRequest.cs

Source:PageAddScriptToEvaluateOnNewDocumentRequest.cs Github

copy

Full Screen

1namespace PuppeteerSharp.Messaging2{3    internal class PageAddScriptToEvaluateOnNewDocumentRequest4    {5        public string Source { get; set; }6        public string WorldName { get; set; }7    }8}...

Full Screen

Full Screen

PageAddScriptToEvaluateOnNewDocumentRequest

Using AI Code Generation

copy

Full Screen

1PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();2pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";3await page.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);4PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();5pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";6await page.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);7PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();8pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";9await page.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);10PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();11pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";12await page.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);13PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();14pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";15await page.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);16PageAddScriptToEvaluateOnNewDocumentRequest pageAddScriptToEvaluateOnNewDocumentRequest = new PageAddScriptToEvaluateOnNewDocumentRequest();17pageAddScriptToEvaluateOnNewDocumentRequest.Script = "window.alert('Hello');";18await page.SendAsync(page

Full Screen

Full Screen

PageAddScriptToEvaluateOnNewDocumentRequest

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2{3    Script = @"() => {4        Object.defineProperty(navigator, 'webdriver', {5            get: () => undefined6        })7    }"8};9await page.Client.SendAsync(request);10var page = await browser.NewPageAsync();11{12    Script = @"() => {13        Object.defineProperty(navigator, 'webdriver', {14            get: () => undefined15        })16    }"17};18await page.Client.SendAsync(request);19var page = await browser.NewPageAsync();20{21    Script = @"() => {22        Object.defineProperty(navigator, 'webdriver', {23            get: () => undefined24        })25    }"26};27await page.Client.SendAsync(request);28var page = await browser.NewPageAsync();29{30    Script = @"() => {31        Object.defineProperty(navigator, 'webdriver', {32            get: () => undefined33        })34    }"35};36await page.Client.SendAsync(request);37var page = await browser.NewPageAsync();38{39    Script = @"() => {40        Object.defineProperty(navigator, 'webdriver', {41            get: () => undefined

Full Screen

Full Screen

PageAddScriptToEvaluateOnNewDocumentRequest

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp.Messaging;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9            var browser = await Puppeteer.LaunchAsync(new LaunchOptions10            {11            });12            var page = await browser.NewPageAsync();13            {14                Script = "window.addEventListener('click', function (e) { console.log(e.target); });"15            };16            await page.Client.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);17            await page.WaitForTimeoutAsync(5000);18            await browser.CloseAsync();19        }20    }21}22using PuppeteerSharp.Messaging;23using System;24using System.Threading.Tasks;25{26    {27        static async Task Main(string[] args)28        {29            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);30            var browser = await Puppeteer.LaunchAsync(new LaunchOptions31            {32            });33            var page = await browser.NewPageAsync();34            {35                Script = "window.addEventListener('click', function (e) { console.log(e.target); });"36            };37            await page.Client.SendAsync(pageAddScriptToEvaluateOnNewDocumentRequest);38            await page.WaitForTimeoutAsync(5000);39            await browser.CloseAsync();40        }41    }42}43using PuppeteerSharp.Messaging;44using System;45using System.Threading.Tasks;46{47    {48        static async Task Main(string[] args)49        {50            await new BrowserFetcher().DownloadAsync(B

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful