Class BrowserContext
- Namespace
- PuppeteerSharp
- Assembly
- PuppeteerSharp.dll
Supports all classes in the .NET class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all .NET classes; it is the root of the type hierarchy.
public abstract class BrowserContext : IBrowserContext, IDisposable, IAsyncDisposable
- Inheritance
-
BrowserContext
- Implements
- Derived
Properties
Browser
public Browser Browser { get; protected init; }
Property Value
Id
Browser Context Id.
public string Id { get; protected init; }
Property Value
IsClosed
Whether this IBrowserContext is closed.
public bool IsClosed { get; }
Property Value
Methods
ClearPermissionOverridesAsync()
Clears all permission overrides for the browser context.
public abstract Task ClearPermissionOverridesAsync()
Returns
- Task
The task.
CloseAsync()
Closes the browser context. All the targets that belong to the browser context will be closed.
public abstract Task CloseAsync()
Returns
- Task
Task.
DeleteCookieAsync(params CookieParam[])
Removes cookies from the browser context.
public Task DeleteCookieAsync(params CookieParam[] cookies)
Parameters
cookiesCookieParam[]Complete CookieParam objects to be removed.
Returns
- Task
Task.
DeleteMatchingCookiesAsync(params DeleteCookiesRequest[])
Deletes cookies matching the provided filters in this browser context.
public Task DeleteMatchingCookiesAsync(params DeleteCookiesRequest[] filters)
Parameters
filtersDeleteCookiesRequest[]Filters to match cookies against.
Returns
- Task
Task.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Closes the browser context. All the targets that belong to the browser context will be closed.
protected virtual void Dispose(bool disposing)
Parameters
DisposeAsync()
Closes the browser context. All the targets that belong to the browser context will be closed.
public ValueTask DisposeAsync()
Returns
- ValueTask
ValueTask.
GetCookiesAsync()
Gets all cookies in the browser context.
public abstract Task<CookieParam[]> GetCookiesAsync()
Returns
- Task<CookieParam[]>
Task which resolves to an array of CookieParam.
NewPageAsync(CreatePageOptions)
Creates a new page.
public abstract Task<IPage> NewPageAsync(CreatePageOptions options = null)
Parameters
optionsCreatePageOptionsOptions for creating the page.
Returns
OverridePermissionsAsync(string, IEnumerable<OverridePermission>)
Overrides the browser context permissions.
public abstract Task OverridePermissionsAsync(string origin, IEnumerable<OverridePermission> permissions)
Parameters
originstringThe origin to grant permissions to, e.g. "https://example.com".
permissionsIEnumerable<OverridePermission>An array of permissions to grant. All permissions that are not listed here will be automatically denied.
Returns
- Task
The task.
Examples
var context = browser.DefaultBrowserContext; await context.OverridePermissionsAsync("https://html5demos.com", new List<string> {"geolocation"});
- See Also
PagesAsync(bool)
An array of all pages inside the browser context.
public abstract Task<IPage[]> PagesAsync(bool includeAll = false)
Parameters
includeAllboolExperimental. When true, includes all kinds of pages.
Returns
- Task<IPage[]>
Task which resolves to an array of all open pages. Non visible pages, such as
"background_page", will not be listed here. You can find them using PageAsync().
SetCookieAsync(params CookieData[])
Sets cookies in the browser context.
public abstract Task SetCookieAsync(params CookieData[] cookies)
Parameters
cookiesCookieData[]Cookies to set.
Returns
- Task
Task.
SetPermissionAsync(string, params PermissionEntry[])
Sets the permission for a specific origin.
public abstract Task SetPermissionAsync(string origin, params PermissionEntry[] permissions)
Parameters
originstringThe origin to set the permission for, e.g. "https://example.com". Use "*" for all origins (CDP only).
permissionsPermissionEntry[]The permissions to set.
Returns
- Task
The task.
Targets()
Gets an array of all active targets inside the browser context.
public abstract ITarget[] Targets()
Returns
- ITarget[]
An array of all active targets inside the browser context.
WaitForTargetAsync(Func<ITarget, bool>, WaitForOptions)
This searches for a target in this specific browser context.
await page.EvaluateAsync("() => window.open('https://www.example.com/')");
var newWindowTarget = await browserContext.WaitForTargetAsync((target) => target.Url == "https://www.example.com/");
public Task<ITarget> WaitForTargetAsync(Func<ITarget, bool> predicate, WaitForOptions options = null)
Parameters
predicateFunc<ITarget, bool>A function to be run for every target.
optionsWaitForOptionsoptions.
Returns
Events
TargetChanged
Raised when the url of a target changes
public event EventHandler<TargetChangedArgs> TargetChanged
Event Type
TargetCreated
Raised when a target is created, for example when a new page is opened by window.openhttps://developer.mozilla.org/en-US/docs/Web/API/Window/open or NewPageAsync(CreatePageOptions).
public event EventHandler<TargetChangedArgs> TargetCreated
Event Type
TargetDestroyed
Raised when a target is destroyed, for example when a page is closed
public event EventHandler<TargetChangedArgs> TargetDestroyed