Best Puppeteer-sharp code snippet using PuppeteerSharp.NetworkManager
Page.cs
Source:Page.cs  
...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,...FrameManager.cs
Source:FrameManager.cs  
...23            Client = client;24            Page = page;25            _frames = new ConcurrentDictionary<string, Frame>();26            _contextIdToContext = new Dictionary<int, ExecutionContext>();27            NetworkManager = new NetworkManager(client, ignoreHTTPSErrors, this);28            TimeoutSettings = timeoutSettings;29            _asyncFrames = new AsyncDictionaryHelper<string, Frame>(_frames, "Frame {0} not found");30            Client.MessageReceived += Client_MessageReceived;31        }32        #region Properties33        internal event EventHandler<FrameEventArgs> FrameAttached;34        internal event EventHandler<FrameEventArgs> FrameDetached;35        internal event EventHandler<FrameEventArgs> FrameNavigated;36        internal event EventHandler<FrameEventArgs> FrameNavigatedWithinDocument;37        internal event EventHandler<FrameEventArgs> LifecycleEvent;38        internal CDPSession Client { get; }39        internal NetworkManager NetworkManager { get; }40        internal Frame MainFrame { get; set; }41        internal Page Page { get; }42        internal TimeoutSettings TimeoutSettings { get; }43        #endregion44        #region Public Methods45        internal static async Task<FrameManager> CreateFrameManagerAsync(46            CDPSession client,47            Page page,48            bool ignoreHTTPSErrors,49            TimeoutSettings timeoutSettings)50        {51            var frameManager = new FrameManager(client, page, ignoreHTTPSErrors, timeoutSettings);52            var getFrameTreeTask = client.SendAsync<PageGetFrameTreeResponse>("Page.getFrameTree");53            await Task.WhenAll(54                client.SendAsync("Page.enable"),55                getFrameTreeTask).ConfigureAwait(false);56            await frameManager.HandleFrameTreeAsync(new FrameTree(getFrameTreeTask.Result.FrameTree)).ConfigureAwait(false);57            await Task.WhenAll(58                client.SendAsync("Page.setLifecycleEventsEnabled", new PageSetLifecycleEventsEnabledRequest { Enabled = true }),59                client.SendAsync("Runtime.enable"),60                frameManager.NetworkManager.InitializeAsync()).ConfigureAwait(false);61            await frameManager.EnsureIsolatedWorldAsync().ConfigureAwait(false);62            return frameManager;63        }64        internal ExecutionContext ExecutionContextById(int contextId)65        {66            _contextIdToContext.TryGetValue(contextId, out var context);67            return context;68        }69        public async Task<Response> NavigateFrameAsync(Frame frame, string url, NavigationOptions options)70        {71            var referrer = string.IsNullOrEmpty(options.Referer)72               ? NetworkManager.ExtraHTTPHeaders?.GetValueOrDefault(RefererHeaderName)73               : options.Referer;74            var requests = new Dictionary<string, Request>();75            var timeout = options?.Timeout ?? TimeoutSettings.NavigationTimeout;76            using (var watcher = new LifecycleWatcher(this, frame, options?.WaitUntil, timeout))77            {78                try79                {80                    var navigateTask = NavigateAsync(Client, url, referrer, frame.Id);81                    var task = await Task.WhenAny(82                        watcher.TimeoutOrTerminationTask,83                        navigateTask).ConfigureAwait(false);84                    await task.ConfigureAwait(false);85                    task = await Task.WhenAny(86                        watcher.TimeoutOrTerminationTask,...NetworkManager.cs
Source:NetworkManager.cs  
...7using PuppeteerSharp.Helpers.Json;8using PuppeteerSharp.Messaging;9namespace PuppeteerSharp10{11    internal class NetworkManager12    {13        #region Private members14        private readonly CDPSession _client;15        private readonly ConcurrentDictionary<string, Request> _requestIdToRequest = new ConcurrentDictionary<string, Request>();16        private readonly ConcurrentDictionary<string, RequestWillBeSentPayload> _requestIdToRequestWillBeSentEvent =17            new ConcurrentDictionary<string, RequestWillBeSentPayload>();18        private readonly ConcurrentDictionary<string, string> _requestIdToInterceptionId = new ConcurrentDictionary<string, string>();19        private readonly ILogger _logger;20        private Dictionary<string, string> _extraHTTPHeaders;21        private bool _offine;22        private Credentials _credentials;23        private List<string> _attemptedAuthentications = new List<string>();24        private bool _userRequestInterceptionEnabled;25        private bool _protocolRequestInterceptionEnabled;26        private bool _ignoreHTTPSErrors;27        private bool _userCacheDisabled;28        #endregion29        internal NetworkManager(CDPSession client, bool ignoreHTTPSErrors, FrameManager frameManager)30        {31            FrameManager = frameManager;32            _client = client;33            _ignoreHTTPSErrors = ignoreHTTPSErrors;34            _client.MessageReceived += Client_MessageReceived;35            _logger = _client.Connection.LoggerFactory.CreateLogger<NetworkManager>();36        }37        #region Public Properties38        internal Dictionary<string, string> ExtraHTTPHeaders => _extraHTTPHeaders?.Clone();39        internal event EventHandler<ResponseCreatedEventArgs> Response;40        internal event EventHandler<RequestEventArgs> Request;41        internal event EventHandler<RequestEventArgs> RequestFinished;42        internal event EventHandler<RequestEventArgs> RequestFailed;43        internal FrameManager FrameManager { get; set; }44        #endregion45        #region Public Methods46        internal async Task InitializeAsync()47        {48            await _client.SendAsync("Network.enable").ConfigureAwait(false);49            if (_ignoreHTTPSErrors)50            {51                await _client.SendAsync("Security.setIgnoreCertificateErrors", new SecuritySetIgnoreCertificateErrorsRequest52                {53                    Ignore = true54                }).ConfigureAwait(false);55            }56        }57        internal Task AuthenticateAsync(Credentials credentials)58        {59            _credentials = credentials;60            return UpdateProtocolRequestInterceptionAsync();61        }62        internal Task SetExtraHTTPHeadersAsync(Dictionary<string, string> extraHTTPHeaders)63        {64            _extraHTTPHeaders = new Dictionary<string, string>();65            foreach (var item in extraHTTPHeaders)66            {67                _extraHTTPHeaders[item.Key.ToLower()] = item.Value;68            }69            return _client.SendAsync("Network.setExtraHTTPHeaders", new NetworkSetExtraHTTPHeadersRequest70            {71                Headers = _extraHTTPHeaders72            });73        }74        internal async Task SetOfflineModeAsync(bool value)75        {76            if (_offine != value)77            {78                _offine = value;79                await _client.SendAsync("Network.emulateNetworkConditions", new NetworkEmulateNetworkConditionsRequest80                {81                    Offline = value,82                    Latency = 0,83                    DownloadThroughput = -1,84                    UploadThroughput = -185                }).ConfigureAwait(false);86            }87        }88        internal Task SetUserAgentAsync(string userAgent)89            => _client.SendAsync("Network.setUserAgentOverride", new NetworkSetUserAgentOverrideRequest90            {91                UserAgent = userAgent92            });93        internal Task SetCacheEnabledAsync(bool enabled)94        {95            _userCacheDisabled = !enabled;96            return UpdateProtocolCacheDisabledAsync();97        }98        internal Task SetRequestInterceptionAsync(bool value)99        {100            _userRequestInterceptionEnabled = value;101            return UpdateProtocolRequestInterceptionAsync();102        }103        #endregion104        #region Private Methods105        private Task UpdateProtocolCacheDisabledAsync()106            => _client.SendAsync("Network.setCacheDisabled", new NetworkSetCacheDisabledRequest107            {108                CacheDisabled = _userCacheDisabled || _protocolRequestInterceptionEnabled109            });110        private async void Client_MessageReceived(object sender, MessageEventArgs e)111        {112            try113            {114                switch (e.MessageID)115                {116                    case "Fetch.requestPaused":117                        await OnRequestPausedAsync(e.MessageData.ToObject<FetchRequestPausedResponse>(true));118                        break;119                    case "Fetch.authRequired":120                        await OnAuthRequiredAsync(e.MessageData.ToObject<FetchAuthRequiredResponse>(true));121                        break;122                    case "Network.requestWillBeSent":123                        await OnRequestWillBeSentAsync(e.MessageData.ToObject<RequestWillBeSentPayload>(true));124                        break;125                    case "Network.requestServedFromCache":126                        OnRequestServedFromCache(e.MessageData.ToObject<RequestServedFromCacheResponse>(true));127                        break;128                    case "Network.responseReceived":129                        OnResponseReceived(e.MessageData.ToObject<ResponseReceivedResponse>(true));130                        break;131                    case "Network.loadingFinished":132                        OnLoadingFinished(e.MessageData.ToObject<LoadingFinishedResponse>(true));133                        break;134                    case "Network.loadingFailed":135                        OnLoadingFailed(e.MessageData.ToObject<LoadingFailedResponse>(true));136                        break;137                }138            }139            catch (Exception ex)140            {141                var message = $"NetworkManager failed to process {e.MessageID}. {ex.Message}. {ex.StackTrace}";142                _logger.LogError(ex, message);143                _client.Close(message);144            }145        }146        private void OnLoadingFailed(LoadingFailedResponse e)147        {148            // For certain requestIds we never receive requestWillBeSent event.149            // @see https://crbug.com/750469150            if (_requestIdToRequest.TryGetValue(e.RequestId, out var request))151            {152                request.Failure = e.ErrorText;153                request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);154                _requestIdToRequest.TryRemove(request.RequestId, out _);155                if (request.InterceptionId != null)...LifecycleWatcher.cs
Source:LifecycleWatcher.cs  
...54            _terminationCancellationToken = new CancellationTokenSource();55            frameManager.LifecycleEvent += FrameManager_LifecycleEvent;56            frameManager.FrameNavigatedWithinDocument += NavigatedWithinDocument;57            frameManager.FrameDetached += OnFrameDetached;58            frameManager.NetworkManager.Request += OnRequest;59            frameManager.Client.Disconnected += OnClientDisconnected;60            CheckLifecycleComplete();61        }62        #region Properties63        public Task<bool> SameDocumentNavigationTask => _sameDocumentNavigationTaskWrapper.Task;64        public Task<bool> NewDocumentNavigationTask => _newDocumentNavigationTaskWrapper.Task;65        public Response NavigationResponse => _navigationRequest?.Response;66        public Task TimeoutOrTerminationTask => _terminationTaskWrapper.Task.WithTimeout(_timeout, cancellationToken: _terminationCancellationToken.Token);67        public Task LifecycleTask => _lifecycleTaskWrapper.Task;68        #endregion69        #region Private methods70        private void OnClientDisconnected(object sender, EventArgs e)71            => Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _frameManager.Client.CloseReason));72        private void FrameManager_LifecycleEvent(object sender, FrameEventArgs e) => CheckLifecycleComplete();73        private void OnFrameDetached(object sender, FrameEventArgs e)74        {75            var frame = e.Frame;76            if (_frame == frame)77            {78                Terminate(new PuppeteerException("Navigating frame was detached"));79                return;80            }81            CheckLifecycleComplete();82        }83        private void CheckLifecycleComplete()84        {85            // We expect navigation to commit.86            if (!CheckLifecycle(_frame, _expectedLifecycle))87            {88                return;89            }90            _lifecycleTaskWrapper.TrySetResult(true);91            if (_frame.LoaderId == _initialLoaderId && !_hasSameDocumentNavigation)92            {93                return;94            }95            if (_hasSameDocumentNavigation)96            {97                _sameDocumentNavigationTaskWrapper.TrySetResult(true);98            }99            if (_frame.LoaderId != _initialLoaderId)100            {101                _newDocumentNavigationTaskWrapper.TrySetResult(true);102            }103        }104        private void Terminate(PuppeteerException ex) => _terminationTaskWrapper.TrySetException(ex);105        private void OnRequest(object sender, RequestEventArgs e)106        {107            if (e.Request.Frame != _frame || !e.Request.IsNavigationRequest)108            {109                return;110            }111            _navigationRequest = e.Request;112        }113        private void NavigatedWithinDocument(object sender, FrameEventArgs e)114        {115            if (e.Frame != _frame)116            {117                return;118            }119            _hasSameDocumentNavigation = true;120            CheckLifecycleComplete();121        }122        private bool CheckLifecycle(Frame frame, IEnumerable<string> expectedLifecycle)123        {124            foreach (var item in expectedLifecycle)125            {126                if (!frame.LifecycleEvents.Contains(item))127                {128                    return false;129                }130            }131            foreach (var child in frame.ChildFrames)132            {133                if (!CheckLifecycle(child, expectedLifecycle))134                {135                    return false;136                }137            }138            return true;139        }140        public void Dispose() => Dispose(true);141        ~LifecycleWatcher() => Dispose(false);142        public void Dispose(bool disposing)143        {144            _frameManager.LifecycleEvent -= FrameManager_LifecycleEvent;145            _frameManager.FrameNavigatedWithinDocument -= NavigatedWithinDocument;146            _frameManager.FrameDetached -= OnFrameDetached;147            _frameManager.NetworkManager.Request -= OnRequest;148            _frameManager.Client.Disconnected -= OnClientDisconnected;149            _terminationCancellationToken.Cancel();150        }151        #endregion152    }153}...NavigatorWatcher.cs
Source:NavigatorWatcher.cs  
...30        public NavigatorWatcher(31            CDPSession client,32            FrameManager frameManager,33            Frame mainFrame,34            NetworkManager networkManager,35            int timeout,36            NavigationOptions options)37        {38            var waitUntil = new[] { WaitUntilNavigation.Load };39            if (options?.WaitUntil != null)40            {41                waitUntil = options.WaitUntil;42            }43            _expectedLifecycle = waitUntil.Select(w =>44            {45                var protocolEvent = _puppeteerToProtocolLifecycle.GetValueOrDefault(w);46                Contract.Assert(protocolEvent != null, $"Unknown value for options.waitUntil: {w}");47                return protocolEvent;48            });...NetworkManager
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {s)6        {7            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultReviion;8            var browser = await Puppeteer.LaunchAsync(new LaunchOptions9            });10            var page = await browser.NewPageAsync();11        {await page.ScreenshotAsync("google.png");12            await browser.loseAsync();13        }14    }15}NetworkManager
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            var browser = await Puppeteer.LaunchAsync(new LaunchOptions9            {10            });11            var page = await browser.NewPageAsync();12            await page.ScreenshotAsync("google.png");13            await browser.CloseAsync();14        }15    }16}NetworkManager
Using AI Code Generation
1using PuppeteerSharp;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            await page.ScreenshotAsync("google.png");14            await browser.CloseAsync();15        }16    }17}NetworkManager
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static async Task Main(string[] args)7        {8            Console.WriteLine("Hello World!");9            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);10            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions11            {12            }))13            {14                var page = await browser.NewPageAsync();15                await page.ScreenshotAsync("google.png");16            }17        }18    }19}20using PuppeteerSharp;21using System;22using System.Threading.Tasks;23{24    {25        static async Task Main(string[] args)26        {27            Console.WriteLine("Hello World!");28            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);29            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions30            {31            }))32            {33                var page = await browser.NewPageAsync();34                await page.ScreenshotAsync("google.png");35            }evision);36            using (var browserNetworkManager
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static void Main(string[] args)7        {8            Console.WriteLine("Hello World!");9            MainAsync().Wait();10        }11        static async Task MainAsync()12        {13            {14                Args = new string[] { "--start-maximized" }15            };16            using (var browser = await Puppeteer.LaunchAsync(options))17            using (var page = await browser.NewPageAsync())18            {19                await page.ScreenshotAsync("google.png");20            }21        }22    }23}24using PuppeteerSharp;25using System;26using System.Threading.Tasks;27{28    {29        static void Main(string[] args)30        {31            Console.WriteLine("Hello World!");32            MainAsync().Wait();33        }34        static async Task MainAsync()35        {36            {37                Args = new string[] { "--start-maximized" }38            };39            using (var browser = await Puppeteer.LaunchAsync(options))40            using (var page = await browser.NewPageAsync())41            {42                await page.ScreenshotAsync("google.png");43            }44        }45    }46}47using PuppeteerSharp;48using System;49using System.Threading.Tasks;50{51    {52        static oid Main(strng[] args)53        {54            Conole.WriteLne("Hello Wrld!");55            MainAsyc().Wait(56        }57        static async Task MainAsync()58        {59            var}options=newLanchOption60            {61                Args = new string[] { "--start-maximized" }62            };63            us = await Puppeteer.LaunchAsync(options))64            using (var page = await browser.NewPageAsync())65            {66                await page.ScreenshotAsync("google.png");67            }68        }69}70using PuppeteerSharp;71using System;72using System.Threading.Tasks;NetworkManager
Using AI Code Generation
1/{2 ====== static async Task Main(string[] args)3        {4            Console.WriteLine("Hello World!");5            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);6            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions7            {8            }))9            {10                var page = await browser.NewPageAsync();11                await page.ScreenshotAsync("google.png");12            }13        }14    }15}16using PuppeteerSharp;17using System;18using System.Threading.Tasks;19{20    {21        static async Task Main(string[] args)22        {23            Console.WriteLine("Hello World!");24            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);25            using (var browserNetworkManager
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5    {6        static void Main(string[] args)7        {8            Console.WriteLine("Hello World!");9            MainAsync().Wait();10        }11        static async Task MainAsync()12        {13            {14                Args = new string[] { "--start-maximized" }15            };16            using (var browser = await Puppeteer.LaunchAsync(options))17            using (var page = await browser.NewPageAsync())18            {19                await page.ScreenshotAsync("google.png");20            }21        }22    }23}24using PuppeteerSharp;25using System;26using System.Threading.Tasks;ertificates;NetworkManager
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5    {6        static void Main(string[] args)7        {8            MainAsync().GetAwaiter().GetResult();9        }10        static async Task MainAsync()11        {12            var networkManager = new NetworkManager();13            networkManager.Request += NetworkManager_Request;14            networkManager.Response += NetworkManager_Response;15            usng (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))16            {17                using (var page = await browser.NewPageAsync())18                {19                    page.NetworkManager = networkManager;20                    Conole.ReadLine()21                }22            }23        }24        private static void NetworkManager_Response(object sender, ResponseCreatedEventArgs e)25        {26            var response = e.Response;27            var request = response.Request;28            Console.WriteLine(request.Url + " " + response.Stat.ToStr());29        }30       private static void NetworkManager_Request(object sender, RequestEventArgs e)31        {32            var request = e.Request;33            Console.WriteLine(request.Url);34        }35    }36}37using System;38using Sstem.Threading.Task;39using PuppeerSharp;40{41    {42        static void Main(string[] args)43        {44            MainAsync().GetAwaiter().GetResult();45        }NetworkManager
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5    {6        static void Main(string[] args)7        {8            MainAsync().GetAwaiter().GetResult();9        }10        static async Task MainAsync()11        {12            var networkManager = new NetworkManager();13            networkManager.Request += NetworkManager_Request;14            networkManager.Response += NetworkManager_Response;15            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))16            {17                using (var page = await browser.NewPageAsync())18                {19                    page.NetworkManager = networkManager;20                    Console.ReadLine();21                }22            }23        }24        private static void NetworkManager_Response(object sender, ResponseCreatedEventArgs e)25        {26            var response = e.Response;27            var request = response.Request;28            Console.WriteLine(request.Url + " " + response.Status.ToString());29        }30        private static void NetworkManager_Request(object sender, RequestEventArgs e)31        {32            var request = e.Request;33            Console.WriteLine(request.Url);34        }35    }36}37using System;38using System.Threading.Tasks;39using PuppeteerSharp;40{41    {42        static void Main(string[] args)43        {44            MainAsync().GetAwaiter().GetResult();45        }46{47    {48        static void Main(string[] args)49        {50            Console.WriteLine("Hello World!");51            MainAsync().Wait();52        }53        static async Task MainAsync()54        {55            {56                Args = new string[] { "--start-maximized" }57            };58            using (var browser = await Puppeteer.LaunchAsync(options))59            using (var page = await browser.NewPageAsync())60            {61                await page.ScreenshotAsync("google.png");62            }63        }64    }65}66using PuppeteerSharp;67using System;68using System.Threading.Tasks;69{70    {71        static void Main(string[] args)72        {73            Console.WriteLine("Hello World!");74            MainAsync().Wait();75        }76        static async Task MainAsync()77        {78            {79                Args = new string[] { "--start-maximized" }80            };81            using (var browser = await Puppeteer.LaunchAsync(options))82            using (var page = await browser.NewPageAsync())83            {84                await page.ScreenshotAsync("google.png");85            }86        }NetworkManager
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5    {6        static async Task Main(string[] args)7        {8            Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions9            {10            });11            Page page = await browser.NewPageAsync();12            string html = await page.GetContentAsync();13            Console.WriteLine(html);14            System.IO.File.WriteAllText("html.txt", html);15            await browser.CloseAsync();16            Environment.Exit(0);17        }18    }19}20using System;21using System.Threading.Tasks;22using PuppeteerSharp;23{24    {25        static async Task Main(string[] args)26        {27            Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions28            {29            });30            Page page = await browser.NewPageAsync();31            string html = await page.GetContentAsync();32            Console.WriteLine(html);33            System.IO.File.WriteAllText("html.txt", html);34            await browser.CloseAsync();35            Environment.Exit(0);36        }37    }38}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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
