Class Page
Provides methods to interact with a single tab in Chromium. One IBrowser instance might have multiple IPage instances.
Inheritance
Namespace: PuppeteerSharp
Assembly: PuppeteerSharp.dll
Syntax
public class Page : IPage, IDisposable
Fields
| Request doc improvement View SourceSupportedMetrics
List of supported metrics.
Declaration
public static readonly IEnumerable<string> SupportedMetrics
Field Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.String> |
Properties
| Request doc improvement View SourceAccessibility
Gets the accessibility.
Declaration
public IAccessibility Accessibility { get; }
Property Value
Type | Description |
---|---|
IAccessibility |
BrowserContext
Get the browser context that the page belongs to.
Declaration
public IBrowserContext BrowserContext { get; }
Property Value
Type | Description |
---|---|
IBrowserContext |
Client
The CDPSession instances are used to talk raw Chrome Devtools Protocol:
- Protocol methods can be called with SendAsync(String, Object, Boolean) method.
- Protocol events, using the MessageReceived event.
Documentation on DevTools Protocol can be found here: https://chromedevtools.github.io/devtools-protocol/.
var client = await Page.Target.CreateCDPSessionAsync();
await client.SendAsync("Animation.enable");
client.MessageReceived += (sender, e) =>
{
if (e.MessageID == "Animation.animationCreated")
{
Console.WriteLine("Animation created!");
}
};
JObject response = await client.SendAsync("Animation.getPlaybackRate");
Console.WriteLine("playback rate is " + response.playbackRate);
await client.SendAsync("Animation.setPlaybackRate", new
{
playbackRate = Convert.ToInt32(response.playbackRate / 2)
});
Declaration
public CDPSession Client { get; }
Property Value
Type | Description |
---|---|
CDPSession |
Coverage
Gets this page's coverage.
Declaration
public ICoverage Coverage { get; }
Property Value
Type | Description |
---|---|
ICoverage |
DefaultNavigationTimeout
This setting will change the default maximum time for the following methods:
Declaration
public int DefaultNavigationTimeout { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
DefaultTimeout
This setting will change the default maximum times for the following methods:
- GoBackAsync(NavigationOptions)
- GoForwardAsync(NavigationOptions)
- GoToAsync(String, NavigationOptions)
- ReloadAsync(NavigationOptions)
- SetContentAsync(String, NavigationOptions)
- WaitForFunctionAsync(String, Object[])
- WaitForNavigationAsync(NavigationOptions)
- WaitForRequestAsync(String, WaitForOptions)
- WaitForResponseAsync(String, WaitForOptions)
- WaitForSelectorAsync(String, WaitForSelectorOptions)
- WaitForExpressionAsync(String, WaitForFunctionOptions).
Declaration
public int DefaultTimeout { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Frames
Gets all frames attached to the page.
Declaration
public IFrame[] Frames { get; }
Property Value
Type | Description |
---|---|
IFrame[] | An array of all frames attached to the page. |
IsClosed
Get an indication that the page has been closed.
Declaration
public bool IsClosed { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsDragInterceptionEnabled
true
if drag events are being intercepted, false
otherwise.
Declaration
public bool IsDragInterceptionEnabled { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Keyboard
Gets this page's keyboard.
Declaration
public IKeyboard Keyboard { get; }
Property Value
Type | Description |
---|---|
IKeyboard |
MainFrame
Gets page's main frame.
Declaration
public IFrame MainFrame { get; }
Property Value
Type | Description |
---|---|
IFrame |
Remarks
Page is guaranteed to have a main frame which persists during navigations.
Mouse
Gets this page's mouse.
Declaration
public IMouse Mouse { get; }
Property Value
Type | Description |
---|---|
IMouse |
Touchscreen
Gets this page's touchscreen.
Declaration
public ITouchscreen Touchscreen { get; }
Property Value
Type | Description |
---|---|
ITouchscreen |
Tracing
Gets this page's tracing.
Declaration
public ITracing Tracing { get; }
Property Value
Type | Description |
---|---|
ITracing |
Url
Shortcut for page.MainFrame.Url
.
Declaration
public string Url { get; }
Property Value
Type | Description |
---|---|
System.String |
Viewport
Gets this page's viewport.
Declaration
public ViewPortOptions Viewport { get; }
Property Value
Type | Description |
---|---|
ViewPortOptions |
Workers
Gets all workers in the page.
Declaration
public WebWorker[] Workers { get; }
Property Value
Type | Description |
---|---|
WebWorker[] |
Methods
| Request doc improvement View SourceAddScriptTagAsync(AddTagOptions)
Adds a <script>
tag into the page with the desired url or content.
Declaration
public Task<IElementHandle> AddScriptTagAsync(AddTagOptions options)
Parameters
Type | Name | Description |
---|---|---|
AddTagOptions | options | add script tag options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame. |
Remarks
Shortcut for page.MainFrame.AddScriptTagAsync(options)
.
See Also
| Request doc improvement View SourceAddScriptTagAsync(String)
Adds a <script>
tag into the page with the desired url or content.
Declaration
public Task<IElementHandle> AddScriptTagAsync(string url)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | script url. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame. |
Remarks
Shortcut for page.MainFrame.AddScriptTagAsync(new AddTagOptions { Url = url })
.
AddStyleTagAsync(AddTagOptions)
Adds a <link rel="stylesheet">
tag into the page with the desired url or a <link rel="stylesheet">
tag with the content.
Declaration
public Task<IElementHandle> AddStyleTagAsync(AddTagOptions options)
Parameters
Type | Name | Description |
---|---|---|
AddTagOptions | options | add style tag options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame. |
Remarks
Shortcut for page.MainFrame.AddStyleTagAsync(options)
.
See Also
| Request doc improvement View SourceAddStyleTagAsync(String)
Adds a <link rel="stylesheet">
tag into the page with the desired url or a <link rel="stylesheet">
tag with the content.
Declaration
public Task<IElementHandle> AddStyleTagAsync(string url)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | stylesheel url. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame. |
Remarks
Shortcut for page.MainFrame.AddStyleTagAsync(new AddTagOptions { Url = url })
.
AuthenticateAsync(Credentials)
Provide credentials for http authentication https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication.
Declaration
public Task AuthenticateAsync(Credentials credentials)
Parameters
Type | Name | Description |
---|---|---|
Credentials | credentials | The credentials. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A Task which resolves after the message is sent to the browser. |
Remarks
To disable authentication, pass null
.
BringToFrontAsync()
Brings page to front (activates tab).
Declaration
public Task BringToFrontAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A task that resolves when the message has been sent to Chromium. |
ClickAsync(String, ClickOptions)
Fetches an element with selector
, scrolls it into view if needed, and then uses Mouse to click in the center of the element.
Declaration
public Task ClickAsync(string selector, ClickOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked. |
ClickOptions | options | click options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task which resolves when the element matching |
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
CloseAsync(PageCloseOptions)
Closes the page.
Declaration
public async Task CloseAsync(PageCloseOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
PageCloseOptions | options | Close options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
DeleteCookieAsync(CookieParam[])
Deletes cookies from the page.
Declaration
public async Task DeleteCookieAsync(params CookieParam[] cookies)
Parameters
Type | Name | Description |
---|---|---|
CookieParam[] | cookies | Cookies to delete. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Dispose()
Provides methods to interact with a single tab in Chromium. One IBrowser instance might have multiple IPage instances.
Declaration
public void Dispose()
Dispose(Boolean)
Dispose resources.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | Indicates whether disposal was initiated by Dispose() operation. |
DisposeAsync()
Provides methods to interact with a single tab in Chromium. One IBrowser instance might have multiple IPage instances.
Declaration
public async ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask |
EmulateAsync(DeviceDescriptor)
Emulates given device metrics and user agent.
Declaration
public Task EmulateAsync(DeviceDescriptor options)
Parameters
Type | Name | Description |
---|---|---|
DeviceDescriptor | options | Emulation options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Remarks
This method is a shortcut for calling two methods: SetViewportAsync(ViewPortOptions) SetUserAgentAsync(String, UserAgentMetadata) To aid emulation, puppeteer provides a list of device descriptors which can be obtained via the Devices. 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.
EmulateCPUThrottlingAsync(Nullable<Decimal>)
Enables CPU throttling to emulate slow CPUs.
Declaration
public Task EmulateCPUThrottlingAsync(decimal? factor = null)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Decimal> | factor | Throttling rate as a slowdown factor (1 is no throttle, 2 is 2x slowdown, etc). |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A task that resolves when the message has been sent to the browser. |
EmulateIdleStateAsync(EmulateIdleOverrides)
Emulates the idle state. If no arguments set, clears idle state emulation.
Declaration
public async Task EmulateIdleStateAsync(EmulateIdleOverrides overrides = null)
Parameters
Type | Name | Description |
---|---|---|
EmulateIdleOverrides | overrides |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A task that resolves when the message has been sent to the browser. |
EmulateMediaFeaturesAsync(IEnumerable<MediaFeatureValue>)
Given an array of media feature objects, emulates CSS media features on the page.
Declaration
public Task EmulateMediaFeaturesAsync(IEnumerable<MediaFeatureValue> features)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<MediaFeatureValue> | features | Features to apply. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Emulate features task. |
EmulateMediaTypeAsync(MediaType)
Emulates a media such as screen or print.
Declaration
public Task EmulateMediaTypeAsync(MediaType type)
Parameters
Type | Name | Description |
---|---|---|
MediaType | type | Media to set. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Emulate media type task. |
EmulateNetworkConditionsAsync(NetworkConditions)
Emulates network conditions.
Declaration
public Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions)
Parameters
Type | Name | Description |
---|---|---|
NetworkConditions | networkConditions | Passing |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Result task. |
Remarks
NOTE This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644).
EmulateTimezoneAsync(String)
Changes the timezone of the page.
Declaration
public async Task EmulateTimezoneAsync(string timezoneId)
Parameters
Type | Name | Description |
---|---|---|
System.String | timezoneId | Timezone to set. See ICU’s |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The viewport task. |
EmulateVisionDeficiencyAsync(VisionDeficiency)
Simulates the given vision deficiency on the page.
Declaration
public Task EmulateVisionDeficiencyAsync(VisionDeficiency type)
Parameters
Type | Name | Description |
---|---|---|
VisionDeficiency | type | The type of deficiency to simulate, or None to reset. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A task that resolves when the message has been sent to the browser. |
EvaluateExpressionAsync(String)
Executes a script in browser context.
Declaration
public Task<JToken> EvaluateExpressionAsync(string script)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Script to be evaluated in browser context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<JToken> | Task which resolves to script return value. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
See Also
EvaluateExpressionAsync<T>(String)
Executes a script in browser context.
Declaration
public Task<T> EvaluateExpressionAsync<T>(string script)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Script to be evaluated in browser context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | Task which resolves to script return value. |
Type Parameters
Name | Description |
---|---|
T | The type to deserialize the result to. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
See Also
EvaluateExpressionHandleAsync(String)
Executes a script in browser context.
Declaration
public Task<IJSHandle> EvaluateExpressionHandleAsync(string script)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Script to be evaluated in browser context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | Task which resolves to script return value. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
EvaluateExpressionOnNewDocumentAsync(String)
Adds a function which would be invoked in one of the following scenarios:
- whenever the page is navigated
- whenever the child frame is attached or navigated. In this case, the function is invoked in the context of the newly attached frame.
Declaration
public Task EvaluateExpressionOnNewDocumentAsync(string expression)
Parameters
Type | Name | Description |
---|---|---|
System.String | expression | Javascript expression to be evaluated in browser context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Remarks
The function is invoked after the document was created but before any of its scripts were run. This is useful to amend JavaScript environment, e.g. to seed Math.random
.
EvaluateFunctionAsync(String, Object[])
Executes a function in browser context.
Declaration
public Task<JToken> EvaluateFunctionAsync(string script, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Script to be evaluated in browser context. |
System.Object[] | args | Arguments to pass to script. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<JToken> | Task which resolves to script return value. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. IJSHandle instances can be passed as arguments.
See Also
EvaluateFunctionAsync<T>(String, Object[])
Executes a function in browser context.
Declaration
public Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Script to be evaluated in browser context. |
System.Object[] | args | Arguments to pass to script. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | Task which resolves to script return value. |
Type Parameters
Name | Description |
---|---|
T | The type to deserialize the result to. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. IJSHandle instances can be passed as arguments.
See Also
EvaluateFunctionHandleAsync(String, Object[])
Executes a script in browser context.
Declaration
public Task<IJSHandle> EvaluateFunctionHandleAsync(string pageFunction, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | pageFunction | Script to be evaluated in browser context. |
System.Object[] | args | Function arguments. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | Task which resolves to script return value. |
Remarks
If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. IJSHandle instances can be passed as arguments.
EvaluateFunctionOnNewDocumentAsync(String, Object[])
Adds a function which would be invoked in one of the following scenarios:
- whenever the page is navigated
- whenever the child frame is attached or navigated. In this case, the function is invoked in the context of the newly attached frame.
Declaration
public Task EvaluateFunctionOnNewDocumentAsync(string pageFunction, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | pageFunction | Function to be evaluated in browser context. |
System.Object[] | args | Arguments to pass to |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Remarks
The function is invoked after the document was created but before any of its scripts were run. This is useful to amend JavaScript environment, e.g. to seed Math.random
.
ExposeFunctionAsync(String, Action)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves when puppeteerFunction
completes.
Declaration
public Task ExposeFunctionAsync(string name, Action puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Action | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync(String, Action) survive navigations.
ExposeFunctionAsync<TResult>(String, Func<TResult>)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves to the return value of puppeteerFunction
.
Declaration
public Task ExposeFunctionAsync<TResult>(string name, Func<TResult> puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Func<TResult> | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Type Parameters
Name | Description |
---|---|
TResult | The result of |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync<TResult>(String, Func<TResult>) survive navigations.
ExposeFunctionAsync<T, TResult>(String, Func<T, TResult>)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves to the return value of puppeteerFunction
.
Declaration
public Task ExposeFunctionAsync<T, TResult>(string name, Func<T, TResult> puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Func<T, TResult> | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Type Parameters
Name | Description |
---|---|
T | The parameter of |
TResult | The result of |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync<T, TResult>(String, Func<T, TResult>) survive navigations.
ExposeFunctionAsync<T1, T2, TResult>(String, Func<T1, T2, TResult>)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves to the return value of puppeteerFunction
.
Declaration
public Task ExposeFunctionAsync<T1, T2, TResult>(string name, Func<T1, T2, TResult> puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Func<T1, T2, TResult> | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Type Parameters
Name | Description |
---|---|
T1 | The first parameter of |
T2 | The second parameter of |
TResult | The result of |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync<T1, T2, TResult>(String, Func<T1, T2, TResult>) survive navigations.
ExposeFunctionAsync<T1, T2, T3, TResult>(String, Func<T1, T2, T3, TResult>)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves to the return value of puppeteerFunction
.
Declaration
public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Func<T1, T2, T3, TResult> | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Type Parameters
Name | Description |
---|---|
T1 | The first parameter of |
T2 | The second parameter of |
T3 | The third parameter of |
TResult | The result of |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync<T1, T2, T3, TResult>(String, Func<T1, T2, T3, TResult>) survive navigations.
ExposeFunctionAsync<T1, T2, T3, T4, TResult>(String, Func<T1, T2, T3, T4, TResult>)
Adds a function called name
on the page's window
object.
When called, the function executes puppeteerFunction
in C# and returns a System.Threading.Tasks.Task which resolves to the return value of puppeteerFunction
.
Declaration
public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> puppeteerFunction)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the function on the window object. |
System.Func<T1, T2, T3, T4, TResult> | puppeteerFunction | Callback function which will be called in Puppeteer's context. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Type Parameters
Name | Description |
---|---|
T1 | The first parameter of |
T2 | The second parameter of |
T3 | The third parameter of |
T4 | The fourth parameter of |
TResult | The result of |
Remarks
If the puppeteerFunction
returns a System.Threading.Tasks.Task, it will be awaited.
Functions installed via ExposeFunctionAsync<T1, T2, T3, T4, TResult>(String, Func<T1, T2, T3, T4, TResult>) survive navigations.
FocusAsync(String)
Fetches an element with selector
and focuses it.
Declaration
public Task FocusAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to search for element to focus. If there are multiple elements satisfying the selector, the first will be focused. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task which resolves when the element matching |
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
GetContentAsync()
Gets the full HTML contents of the page, including the doctype.
Declaration
public Task<string> GetContentAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Task which resolves to the HTML content. |
See Also
| Request doc improvement View SourceGetCookiesAsync(String[])
Returns the page's cookies.
Declaration
public async Task<CookieParam[]> GetCookiesAsync(params string[] urls)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | urls | Url's to return cookies for. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<CookieParam[]> | Array of cookies. |
Remarks
If no URLs are specified, this method returns cookies for the current page URL. If URLs are specified, only cookies for those URLs are returned.
GetTitleAsync()
Returns page's title.
Declaration
public Task<string> GetTitleAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | page's title. |
GoBackAsync(NavigationOptions)
Navigate to the previous page in history.
Declaration
public Task<IResponse> GoBackAsync(NavigationOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
NavigationOptions | options | Navigation parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task that resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go back, resolves to null. |
GoForwardAsync(NavigationOptions)
Navigate to the next page in history.
Declaration
public Task<IResponse> GoForwardAsync(NavigationOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
NavigationOptions | options | Navigation parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task that resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go forward, resolves to null. |
GoToAsync(String, NavigationOptions)
Navigates to an url.
Declaration
public Task<IResponse> GoToAsync(string url, NavigationOptions options)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | URL to navigate page to. The url should include scheme, e.g. https://. |
NavigationOptions | options | Navigation parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. |
Remarks
GoToAsync(String, Nullable<Int32>, WaitUntilNavigation[]) will throw an error if:
- there's an SSL error (e.g. in case of self-signed certificates).
- target URL is invalid.
- the
timeout
is exceeded during navigation. - the remote server does not respond or is unreachable.
- the main resource failed to load.
GoToAsync(String, Nullable<Int32>, WaitUntilNavigation[]) will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error". The status code for such responses can be retrieved by calling Status
NOTE GoToAsync(String, Nullable<Int32>, WaitUntilNavigation[]) either throws an error or returns a main resource response. The only exceptions are navigation to
about:blank
or navigation to the same URL with a different hash, which would succeed and returnnull
.
NOTE Headless mode doesn't support navigation to a PDF document. See the
upstream issue .
Shortcut for. GoToAsync(String, Nullable<Int32>, WaitUntilNavigation[])
See Also
GoToAsync(String, WaitUntilNavigation)
Navigates to an url.
Declaration
public Task<IResponse> GoToAsync(string url, WaitUntilNavigation waitUntil)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | URL to navigate page to. The url should include scheme, e.g. https://. |
WaitUntilNavigation | waitUntil | When to consider navigation succeeded. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. |
See Also
GoToAsync(String, Nullable<Int32>, WaitUntilNavigation[])
Navigates to an url.
Declaration
public Task<IResponse> GoToAsync(string url, int? timeout = null, WaitUntilNavigation[] waitUntil = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | URL to navigate page to. The url should include scheme, e.g. https://. |
System.Nullable<System.Int32> | timeout | Maximum navigation time in milliseconds, defaults to 30 seconds, pass |
WaitUntilNavigation[] | waitUntil | When to consider navigation succeeded, defaults to Load. Given an array of WaitUntilNavigation, navigation is considered to be successful after all events have been fired. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. |
See Also
HoverAsync(String)
Fetches an element with selector
, scrolls it into view if needed, and then uses Mouse to hover over the center of the element.
Declaration
public Task HoverAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to search for element to hover. If there are multiple elements satisfying the selector, the first will be hovered. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task which resolves when the element matching |
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
MetricsAsync()
Returns metrics.
Declaration
public async Task<Dictionary<string, decimal>> MetricsAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.Dictionary<System.String, System.Decimal>> | Task which resolves into a list of metrics. |
Remarks
All timestamps are in monotonic time: monotonically increasing time in seconds since an arbitrary point in the past.
PdfAsync(String, PdfOptions)
generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public async Task PdfAsync(string file, PdfOptions options)
Parameters
Type | Name | Description |
---|---|---|
System.String | file | The file path to save the PDF to. paths are resolved using System.IO.Path.GetFullPath(System.String). |
PdfOptions | options | pdf options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A Task which resolves after the PDF is generated. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
PdfAsync(String)
generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public Task PdfAsync(string file)
Parameters
Type | Name | Description |
---|---|---|
System.String | file | The file path to save the PDF to. paths are resolved using System.IO.Path.GetFullPath(System.String). |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A Task which resolves after the PDF is generated. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
PdfDataAsync()
Generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public Task<byte[]> PdfDataAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Byte[]> | Task which resolves to a System.Byte[] containing the PDF data. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
PdfDataAsync(PdfOptions)
Generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public Task<byte[]> PdfDataAsync(PdfOptions options)
Parameters
Type | Name | Description |
---|---|---|
PdfOptions | options | pdf options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Byte[]> | Task which resolves to a System.Byte[] containing the PDF data. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
PdfStreamAsync()
generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public Task<Stream> PdfStreamAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.IO.Stream> | Task which resolves to a System.IO.Stream containing the PDF data. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
PdfStreamAsync(PdfOptions)
Generates a pdf of the page with Print css media. To generate a pdf with Screen media call EmulateMediaTypeAsync(MediaType) with Screen.
Declaration
public async Task<Stream> PdfStreamAsync(PdfOptions options)
Parameters
Type | Name | Description |
---|---|---|
PdfOptions | options | pdf options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.IO.Stream> | Task which resolves to a System.IO.Stream containing the PDF data. |
Remarks
Generating a pdf is currently only supported in Chrome headless.
QueryObjectsAsync(IJSHandle)
The method iterates JavaScript heap and finds all the objects with the given prototype.
Shortcut for page.MainFrame.GetExecutionContextAsync().QueryObjectsAsync(prototypeHandle)
.
Declaration
public async Task<IJSHandle> QueryObjectsAsync(IJSHandle prototypeHandle)
Parameters
Type | Name | Description |
---|---|---|
IJSHandle | prototypeHandle | A handle to the object prototype. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | A task which resolves to a handle to an array of objects with this prototype. |
QuerySelectorAllAsync(String)
Runs document.querySelectorAll
within the page. If no elements match the selector, the return value resolve to System.Array.Empty``1.
Declaration
public Task<IElementHandle[]> QuerySelectorAllAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to query page for. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle[]> | Task which resolves to ElementHandles pointing to the frame elements. |
See Also
QuerySelectorAllHandleAsync(String)
A utility function to be used with EvaluateFunctionAsync<T>(Task<IJSHandle>, String, Object[]).
Declaration
public Task<IJSHandle> QuerySelectorAllHandleAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to query page for. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | Task which resolves to a IJSHandle of |
QuerySelectorAsync(String)
The method runs document.querySelector
within the page. If no element matches the selector, the return value resolve to null
.
Declaration
public Task<IElementHandle> QuerySelectorAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to query page for. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | Task which resolves to IElementHandle pointing to the frame element. |
Remarks
Shortcut for page.MainFrame.QuerySelectorAsync(selector)
.
See Also
ReloadAsync(NavigationOptions)
Reloads the page.
Declaration
public async Task<IResponse> ReloadAsync(NavigationOptions options)
Parameters
Type | Name | Description |
---|---|---|
NavigationOptions | options | Navigation options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. |
See Also
ReloadAsync(Nullable<Int32>, WaitUntilNavigation[])
Reloads the page.
Declaration
public Task<IResponse> ReloadAsync(int? timeout = null, WaitUntilNavigation[] waitUntil = null)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Int32> | timeout | Maximum navigation time in milliseconds, defaults to 30 seconds, pass |
WaitUntilNavigation[] | waitUntil | When to consider navigation succeeded, defaults to Load. Given an array of WaitUntilNavigation, navigation is considered to be successful after all events have been fired. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. |
See Also
| Request doc improvement View SourceScreenshotAsync(String, ScreenshotOptions)
Takes a screenshot of the page.
Declaration
public async Task ScreenshotAsync(string file, ScreenshotOptions options)
Parameters
Type | Name | Description |
---|---|---|
System.String | file | The file path to save the image to. The screenshot type will be inferred from file extension. If path is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. |
ScreenshotOptions | options | Screenshot options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The screenshot task. |
ScreenshotAsync(String)
Takes a screenshot of the page.
Declaration
public Task ScreenshotAsync(string file)
Parameters
Type | Name | Description |
---|---|---|
System.String | file | The file path to save the image to. The screenshot type will be inferred from file extension. If path is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The screenshot task. |
ScreenshotBase64Async()
Takes a screenshot of the page.
Declaration
public Task<string> ScreenshotBase64Async()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Task which resolves to a System.String containing the image data as base64. |
ScreenshotBase64Async(ScreenshotOptions)
Takes a screenshot of the page.
Declaration
public Task<string> ScreenshotBase64Async(ScreenshotOptions options)
Parameters
Type | Name | Description |
---|---|---|
ScreenshotOptions | options | Screenshot options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Task which resolves to a System.String containing the image data as base64. |
ScreenshotDataAsync()
Takes a screenshot of the page.
Declaration
public Task<byte[]> ScreenshotDataAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Byte[]> | Task which resolves to a System.Byte[] containing the image data. |
ScreenshotDataAsync(ScreenshotOptions)
Takes a screenshot of the page.
Declaration
public async Task<byte[]> ScreenshotDataAsync(ScreenshotOptions options)
Parameters
Type | Name | Description |
---|---|---|
ScreenshotOptions | options | Screenshot options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Byte[]> | Task which resolves to a System.Byte[] containing the image data. |
ScreenshotStreamAsync()
Takes a screenshot of the page.
Declaration
public Task<Stream> ScreenshotStreamAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.IO.Stream> | Task which resolves to a System.IO.Stream containing the image data. |
ScreenshotStreamAsync(ScreenshotOptions)
Takes a screenshot of the page.
Declaration
public async Task<Stream> ScreenshotStreamAsync(ScreenshotOptions options)
Parameters
Type | Name | Description |
---|---|---|
ScreenshotOptions | options | Screenshot options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.IO.Stream> | Task which resolves to a System.IO.Stream containing the image data. |
SelectAsync(String, String[])
Triggers a change and input event once all the provided options have been selected. If there's no <select> element matching selector, the method throws an error.
Declaration
public Task<string[]> SelectAsync(string selector, params string[] values)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to query page for. |
System.String[] | values | Values of options to select. If the <select> has the multiple attribute, all values are considered, otherwise only the first one is taken into account. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String[]> | Returns an array of option values that have been successfully selected. |
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
See Also
SetBurstModeOffAsync()
Resets the background color and Viewport after taking Screenshots using BurstMode.
Declaration
public Task SetBurstModeOffAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The burst mode off. |
SetBypassCSPAsync(Boolean)
Toggles bypassing page's Content-Security-Policy.
Declaration
public Task SetBypassCSPAsync(bool enabled)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enabled | sets bypassing of page's Content-Security-Policy. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A Task which resolves after the message is sent to the browser. |
Remarks
CSP bypassing happens at the moment of CSP initialization rather then evaluation. Usually this means that SetBypassCSPAsync(Boolean) should be called before navigating to the domain.
SetCacheEnabledAsync(Boolean)
Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled.
Declaration
public Task SetCacheEnabledAsync(bool enabled = true)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enabled | sets the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
SetContentAsync(String, NavigationOptions)
Sets the HTML markup to the page.
Declaration
public Task SetContentAsync(string html, NavigationOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML markup to assign to the page. |
NavigationOptions | options | The navigations options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
See Also
SetCookieAsync(CookieParam[])
Clears all of the current cookies and then sets the cookies for the page.
Declaration
public async Task SetCookieAsync(params CookieParam[] cookies)
Parameters
Type | Name | Description |
---|---|---|
CookieParam[] | cookies | Cookies to set. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
SetDragInterceptionAsync(Boolean)
Whether to enable drag interception.
Declaration
public Task SetDragInterceptionAsync(bool enabled)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enabled | Interception enabled. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A Task that resolves when the message was confirmed by the browser. |
Remarks
Activating drag interception enables the Input.drag
,
methods This provides the capability to capture drag events emitted
on the page, which can then be used to simulate drag-and-drop.
SetExtraHttpHeadersAsync(Dictionary<String, String>)
Sets extra HTTP headers that will be sent with every request the page initiates.
Declaration
public Task SetExtraHttpHeadersAsync(Dictionary<string, string> headers)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.Dictionary<System.String, System.String> | headers | Additional http headers to be sent with every request. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
SetGeolocationAsync(GeolocationOption)
Sets the page's geolocation.
Declaration
public Task SetGeolocationAsync(GeolocationOption options)
Parameters
Type | Name | Description |
---|---|---|
GeolocationOption | options | Geolocation options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The task. |
Remarks
Consider using OverridePermissionsAsync(String, IEnumerable<OverridePermission>) to grant permissions for the page to read its geolocation.
SetJavaScriptEnabledAsync(Boolean)
Enables/Disables Javascript on the page.
Declaration
public Task SetJavaScriptEnabledAsync(bool enabled)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enabled | Whether or not to enable JavaScript on the page. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
SetOfflineModeAsync(Boolean)
Set offline mode for the page.
Declaration
public Task SetOfflineModeAsync(bool value)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | value | When |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Result task. |
SetRequestInterceptionAsync(Boolean)
Activating request interception enables request.AbortAsync, request.ContinueAsync and request.RespondAsync methods.
Declaration
public Task SetRequestInterceptionAsync(bool value)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | value | Whether to enable request interception.. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The request interception task. |
SetUserAgentAsync(String, UserAgentMetadata)
Sets the user agent to be used in this page.
Declaration
public Task SetUserAgentAsync(string userAgent, UserAgentMetadata userAgentData = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | userAgent | Specific user agent to use in this page. |
UserAgentMetadata | userAgentData | Specific user agent client hint data to use in this page. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
SetViewportAsync(ViewPortOptions)
Sets the viewport. In the case of multiple pages in a single browser, each page can have its own viewport size. 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.
Declaration
public async Task SetViewportAsync(ViewPortOptions viewport)
Parameters
Type | Name | Description |
---|---|---|
ViewPortOptions | viewport | Viewport options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | The viewport task. |
TapAsync(String)
Fetches an element with selector
, scrolls it into view if needed, and then uses Touchscreen to tap in the center of the element.
Declaration
public async Task TapAsync(string selector)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector to search for element to tap. If there are multiple elements satisfying the selector, the first will be clicked. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task which resolves when the element matching |
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
TypeAsync(String, String, TypeOptions)
Sends a keydown
, keypress
/input
, and keyup
event for each character in the text.
Declaration
public Task TypeAsync(string selector, string text, TypeOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector of an element to type into. If there are multiple elements satisfying the selector, the first will be used. |
System.String | text | A text to type into a focused element. |
TypeOptions | options | The options to apply to the type operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task. |
Remarks
To press a special key, like Control
or ArrowDown
use PressAsync(String, PressOptions).
Exceptions
Type | Condition |
---|---|
SelectorException | If there's no element matching |
WaitForExpressionAsync(String, WaitForFunctionOptions)
Waits for an expression to be evaluated to a truthy value.
Declaration
public Task<IJSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Expression to be evaluated in browser context. |
WaitForFunctionOptions | options | Optional waiting parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | A task that resolves when the |
See Also
WaitForFileChooserAsync(WaitForFileChooserOptions)
Waits for a page to open a file picker.
Declaration
public async Task<FileChooser> WaitForFileChooserAsync(WaitForFileChooserOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
WaitForFileChooserOptions | options | Optional waiting parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<FileChooser> | A task that resolves after a page requests a file picker. |
Remarks
In non-headless Chromium, this method results in the native file picker dialog not showing up for the user.
WaitForFrameAsync(Func<IFrame, Boolean>, WaitForOptions)
Waits for a frame.
Declaration
public async Task<IFrame> WaitForFrameAsync(Func<IFrame, bool> predicate, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<IFrame, System.Boolean> | predicate | Function which looks for a matching frame. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IFrame> | A task which resolves when a matching frame was attached to the page. |
WaitForFrameAsync(String, WaitForOptions)
Waits for a frame.
Declaration
public Task<IFrame> WaitForFrameAsync(string url, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | Frame url. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IFrame> | A task which resolves when a matching frame was attached to the page. |
WaitForFunctionAsync(String, WaitForFunctionOptions, Object[])
Waits for a function to be evaluated to a truthy value.
Declaration
public Task<IJSHandle> WaitForFunctionAsync(string script, WaitForFunctionOptions options = null, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Function to be evaluated in browser context. |
WaitForFunctionOptions | options | Optional waiting parameters. |
System.Object[] | args | Arguments to pass to |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | A task that resolves when the |
See Also
WaitForFunctionAsync(String, Object[])
Waits for a function to be evaluated to a truthy value.
Declaration
public Task<IJSHandle> WaitForFunctionAsync(string script, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | script | Function to be evaluated in browser context. |
System.Object[] | args | Arguments to pass to |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IJSHandle> | A task that resolves when the |
WaitForNavigationAsync(NavigationOptions)
This resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will indirectly cause the page to navigate.
Declaration
public Task<IResponse> WaitForNavigationAsync(NavigationOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
NavigationOptions | options | navigation options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | Task which resolves to the main resource response.
In case of multiple redirects, the navigation will resolve with the response of the last redirect.
In case of navigation to a different anchor or navigation due to History API usage, the navigation will resolve with |
Remarks
Usage of the History API
https://developer.mozilla.org/en-US/docs/Web/API/History_API to change the URL is considered a navigation.
WaitForNetworkIdleAsync(WaitForNetworkIdleOptions)
Waits for Network Idle.
Declaration
public async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
WaitForNetworkIdleOptions | options | Optional waiting parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | returns Task which resolves when network is idle. |
WaitForRequestAsync(Func<IRequest, Boolean>, WaitForOptions)
Waits for a request.
Declaration
public async Task<IRequest> WaitForRequestAsync(Func<IRequest, bool> predicate, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<IRequest, System.Boolean> | predicate | Function which looks for a matching request. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IRequest> | A task which resolves when a matching request was made. |
WaitForRequestAsync(String, WaitForOptions)
Waits for a request.
Declaration
public Task<IRequest> WaitForRequestAsync(string url, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | URL to wait for. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IRequest> | A task which resolves when a matching request was made. |
WaitForResponseAsync(Func<IResponse, Boolean>, WaitForOptions)
Waits for a response.
Declaration
public Task<IResponse> WaitForResponseAsync(Func<IResponse, bool> predicate, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<IResponse, System.Boolean> | predicate | Function which looks for a matching response. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | A task which resolves when a matching response is received. |
WaitForResponseAsync(Func<IResponse, Task<Boolean>>, WaitForOptions)
Waits for a response.
Declaration
public async Task<IResponse> WaitForResponseAsync(Func<IResponse, Task<bool>> predicate, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<IResponse, System.Threading.Tasks.Task<System.Boolean>> | predicate | Function which looks for a matching response. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | A task which resolves when a matching response is received. |
WaitForResponseAsync(String, WaitForOptions)
Waits for a response.
Declaration
public Task<IResponse> WaitForResponseAsync(string url, WaitForOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | URL to wait for. |
WaitForOptions | options | Options. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IResponse> | A task which resolves when a matching response is received. |
WaitForSelectorAsync(String, WaitForSelectorOptions)
Waits for a selector to be added to the DOM.
Declaration
public Task<IElementHandle> WaitForSelectorAsync(string selector, WaitForSelectorOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | selector | A selector of an element to wait for. |
WaitForSelectorOptions | options | Optional waiting parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | A task that resolves when element specified by selector string is added to DOM.
Resolves to |
See Also
WaitForTimeoutAsync(Int32)
Waits for a timeout.
Declaration
public Task WaitForTimeoutAsync(int milliseconds)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | milliseconds | The amount of time to wait. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A task that resolves when after the timeout. |
See Also
WaitForXPathAsync(String, WaitForSelectorOptions)
Waits for a xpath selector to be added to the DOM.
Declaration
public Task<IElementHandle> WaitForXPathAsync(string xpath, WaitForSelectorOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | xpath | A xpath selector of an element to wait for. |
WaitForSelectorOptions | options | Optional waiting parameters. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle> | A task which resolves when element specified by xpath string is added to DOM.
Resolves to |
See Also
XPathAsync(String)
Evaluates the XPath expression.
Declaration
public Task<IElementHandle[]> XPathAsync(string expression)
Parameters
Type | Name | Description |
---|---|---|
System.String | expression | Expression to evaluate https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<IElementHandle[]> | Task which resolves to an array of IElementHandle. |
Remarks
Shortcut for page.MainFrame.XPathAsync(expression)
.
Events
| Request doc improvement View SourceClose
Raised when the page closes.
Declaration
public event EventHandler Close
Event Type
Type | Description |
---|---|
System.EventHandler |
Console
Raised when JavaScript within the page calls one of console API methods, e.g. console.log
or console.dir
. Also emitted if the page throws an error or a warning.
The arguments passed into console.log
appear as arguments on the event handler.
Declaration
public event EventHandler<ConsoleEventArgs> Console
Event Type
Type | Description |
---|---|
System.EventHandler<ConsoleEventArgs> |
Dialog
Raised when a JavaScript dialog appears, such as alert
, prompt
, confirm
or beforeunload
. Puppeteer can respond to the dialog via Dialog's Accept(String) or Dismiss() methods.
Declaration
public event EventHandler<DialogEventArgs> Dialog
Event Type
Type | Description |
---|---|
System.EventHandler<DialogEventArgs> |
DOMContentLoaded
Raised when the JavaScript DOMContentLoaded
https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded event is dispatched.
Declaration
public event EventHandler DOMContentLoaded
Event Type
Type | Description |
---|---|
System.EventHandler |
Error
Raised when the page crashes
Declaration
public event EventHandler<ErrorEventArgs> Error
Event Type
Type | Description |
---|---|
System.EventHandler<ErrorEventArgs> |
FrameAttached
Raised when a frame is attached.
Declaration
public event EventHandler<FrameEventArgs> FrameAttached
Event Type
Type | Description |
---|---|
System.EventHandler<FrameEventArgs> |
FrameDetached
Raised when a frame is detached.
Declaration
public event EventHandler<FrameEventArgs> FrameDetached
Event Type
Type | Description |
---|---|
System.EventHandler<FrameEventArgs> |
FrameNavigated
Raised when a frame is navigated to a new url.
Declaration
public event EventHandler<FrameEventArgs> FrameNavigated
Event Type
Type | Description |
---|---|
System.EventHandler<FrameEventArgs> |
Load
Raised when the JavaScript load
https://developer.mozilla.org/en-US/docs/Web/Events/load event is dispatched.
Declaration
public event EventHandler Load
Event Type
Type | Description |
---|---|
System.EventHandler |
Metrics
Raised when the JavaScript code makes a call to console.timeStamp
. For the list of metrics see MetricsAsync().
Declaration
public event EventHandler<MetricEventArgs> Metrics
Event Type
Type | Description |
---|---|
System.EventHandler<MetricEventArgs> |
PageError
Raised when an uncaught exception happens within the page.
Declaration
public event EventHandler<PageErrorEventArgs> PageError
Event Type
Type | Description |
---|---|
System.EventHandler<PageErrorEventArgs> |
Popup
Raised when the page opens a new tab or window.
Declaration
public event EventHandler<PopupEventArgs> Popup
Event Type
Type | Description |
---|---|
System.EventHandler<PopupEventArgs> |
Request
Raised when a page issues a request. The Request object is read-only. In order to intercept and mutate requests, see SetRequestInterceptionAsync(Boolean)
Declaration
public event EventHandler<RequestEventArgs> Request
Event Type
Type | Description |
---|---|
System.EventHandler<RequestEventArgs> |
RequestFailed
Raised when a request fails, for example by timing out.
Declaration
public event EventHandler<RequestEventArgs> RequestFailed
Event Type
Type | Description |
---|---|
System.EventHandler<RequestEventArgs> |
RequestFinished
Raised when a request finishes successfully.
Declaration
public event EventHandler<RequestEventArgs> RequestFinished
Event Type
Type | Description |
---|---|
System.EventHandler<RequestEventArgs> |
RequestServedFromCache
Raised when a request ended up loading from cache.
Declaration
public event EventHandler<RequestEventArgs> RequestServedFromCache
Event Type
Type | Description |
---|---|
System.EventHandler<RequestEventArgs> |
Response
Raised when a Response is received.
Declaration
public event EventHandler<ResponseCreatedEventArgs> Response
Event Type
Type | Description |
---|---|
System.EventHandler<ResponseCreatedEventArgs> |
WorkerCreated
Emitted when a dedicated WebWorker (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) is spawned by the page.
Declaration
public event EventHandler<WorkerEventArgs> WorkerCreated
Event Type
Type | Description |
---|---|
System.EventHandler<WorkerEventArgs> |
WorkerDestroyed
Emitted when a dedicated WebWorker (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) is terminated.
Declaration
public event EventHandler<WorkerEventArgs> WorkerDestroyed
Event Type
Type | Description |
---|---|
System.EventHandler<WorkerEventArgs> |
Explicit Interface Implementations
| Request doc improvement View SourceIPage.Browser
Get the browser the page belongs to.
Declaration
IBrowser IPage.Browser { get; }
Returns
Type | Description |
---|---|
IBrowser |
IPage.Client
The CDPSession instances are used to talk raw Chrome Devtools Protocol:
- Protocol methods can be called with SendAsync(String, Object, Boolean) method.
- Protocol events, using the MessageReceived event.
Documentation on DevTools Protocol can be found here: https://chromedevtools.github.io/devtools-protocol/.
var client = await Page.Target.CreateCDPSessionAsync();
await client.SendAsync("Animation.enable");
client.MessageReceived += (sender, e) =>
{
if (e.MessageID == "Animation.animationCreated")
{
Console.WriteLine("Animation created!");
}
};
JObject response = await client.SendAsync("Animation.getPlaybackRate");
Console.WriteLine("playback rate is " + response.playbackRate);
await client.SendAsync("Animation.setPlaybackRate", new
{
playbackRate = Convert.ToInt32(response.playbackRate / 2)
});
Declaration
ICDPSession IPage.Client { get; }
Returns
Type | Description |
---|---|
ICDPSession |
IPage.Target
Gets that target this page was created from.
Declaration
ITarget IPage.Target { get; }
Returns
Type | Description |
---|---|
ITarget |