BrowserContext
Namespace: PuppeteerSharp
public abstract class BrowserContext : IBrowserContext, IDisposable, IAsyncDisposableSupports 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.
Inheritance: object
Implements: IBrowserContext, IDisposable, IAsyncDisposable
Derived: BidiBrowserContext, CdpBrowserContext
Properties
Section titled “Properties”Browser
Section titled “Browser”public Browser Browser { get; protected init; }Value: Browser
public string Id { get; protected init; }Browser Context Id.
Value: string
IsClosed
Section titled “IsClosed”public bool IsClosed { get; }Whether this <a href=“IBrowserContext”>IBrowserContext</a> is closed.
Value: bool
Events
Section titled “Events”TargetChanged
Section titled “TargetChanged”public event EventHandler<TargetChangedArgs> TargetChangedRaised when the url of a target changes
Value: EventHandler<TargetChangedArgs>
TargetCreated
Section titled “TargetCreated”public event EventHandler<TargetChangedArgs> TargetCreatedRaised when a target is created, for example when a new page is opened by window.open <a href=“https://developer.mozilla.org/en-US/docs/Web/API/Window/open">https://developer.mozilla.org/en-US/docs/Web/API/Window/open</a> or NewPageAsync(CreatePageOptions).
Value: EventHandler<TargetChangedArgs>
TargetDestroyed
Section titled “TargetDestroyed”public event EventHandler<TargetChangedArgs> TargetDestroyedRaised when a target is destroyed, for example when a page is closed
Value: EventHandler<TargetChangedArgs>
Methods
Section titled “Methods”ClearPermissionOverridesAsync()
Section titled “ClearPermissionOverridesAsync()”public abstract Task ClearPermissionOverridesAsync()Clears all permission overrides for the browser context.
Returns: Task — The task.
CloseAsync()
Section titled “CloseAsync()”public abstract Task CloseAsync()Closes the browser context. All the targets that belong to the browser context will be closed.
Returns: Task — Task.
DeleteCookieAsync(params CookieParam[])
Section titled “DeleteCookieAsync(params CookieParam[])”public Task DeleteCookieAsync(params CookieParam[] cookies)Removes cookies from the browser context.
Parameters
| Name | Type | Description |
|---|---|---|
cookies |
CookieParam\[\] |
Complete CookieParam objects to be removed. |
Returns: Task — Task.
DeleteMatchingCookiesAsync(params DeleteCookiesRequest[])
Section titled “DeleteMatchingCookiesAsync(params DeleteCookiesRequest[])”public Task DeleteMatchingCookiesAsync(params DeleteCookiesRequest[] filters)Deletes cookies matching the provided filters in this browser context.
Parameters
| Name | Type | Description |
|---|---|---|
filters |
DeleteCookiesRequest\[\] |
Filters to match cookies against. |
Returns: Task — Task.
Dispose()
Section titled “Dispose()”public void Dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Dispose(bool)
Section titled “Dispose(bool)”protected virtual void Dispose(bool disposing)Closes the browser context. All the targets that belong to the browser context will be closed.
Parameters
| Name | Type | Description |
|---|---|---|
disposing |
bool | Indicates whether disposal was initiated by Dispose() operation. |
DisposeAsync()
Section titled “DisposeAsync()”public ValueTask DisposeAsync()Closes the browser context. All the targets that belong to the browser context will be closed.
Returns: ValueTask — ValueTask.
GetCookiesAsync()
Section titled “GetCookiesAsync()”public abstract Task<CookieParam[]> GetCookiesAsync()Gets all cookies in the browser context.
Returns: Task<CookieParam[]> — Task which resolves to an array of CookieParam.
NewPageAsync(CreatePageOptions)
Section titled “NewPageAsync(CreatePageOptions)”public abstract Task<IPage> NewPageAsync(CreatePageOptions options = null)Creates a new page.
Parameters
| Name | Type | Description |
|---|---|---|
options |
CreatePageOptions | Options for creating the page. |
Returns: Task<IPage> — Task which resolves to a new IPage object.
OverridePermissionsAsync(string, IEnumerable<OverridePermission>)
Section titled “OverridePermissionsAsync(string, IEnumerable<OverridePermission>)”public abstract Task OverridePermissionsAsync(string origin, IEnumerable<OverridePermission> permissions)Overrides the browser context permissions.
Parameters
| Name | Type | Description |
|---|---|---|
origin |
string | The origin to grant permissions to, e.g. “https://example.com”. |
permissions |
IEnumerable<OverridePermission> | An array of permissions to grant. All permissions that are not listed here will be automatically denied. |
Returns: Task — The task.
Example
var context = browser.DefaultBrowserContext; await context.OverridePermissionsAsync(“https://html5demos.com”, new List<string> {“geolocation”});
PagesAsync(bool)
Section titled “PagesAsync(bool)”public abstract Task<IPage[]> PagesAsync(bool includeAll = false)An array of all pages inside the browser context.
Parameters
| Name | Type | Description |
|---|---|---|
includeAll |
bool | Experimental. 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[])
Section titled “SetCookieAsync(params CookieData[])”public abstract Task SetCookieAsync(params CookieData[] cookies)Sets cookies in the browser context.
Parameters
| Name | Type | Description |
|---|---|---|
cookies |
CookieData\[\] |
Cookies to set. |
Returns: Task — Task.
SetPermissionAsync(string, params PermissionEntry[])
Section titled “SetPermissionAsync(string, params PermissionEntry[])”public abstract Task SetPermissionAsync(string origin, params PermissionEntry[] permissions)Sets the permission for a specific origin.
Parameters
| Name | Type | Description |
|---|---|---|
origin |
string | The origin to set the permission for, e.g. “https://example.com”. Use “*” for all origins (CDP only). |
permissions |
PermissionEntry\[\] |
The permissions to set. |
Returns: Task — The task.
Targets()
Section titled “Targets()”public abstract ITarget[] Targets()Gets an array of all active targets inside the browser context.
Returns: ITarget\[\] — An array of all active targets inside the browser context.
WaitForTargetAsync(Func<ITarget, bool>, WaitForOptions)
Section titled “WaitForTargetAsync(Func<ITarget, bool>, WaitForOptions)”public Task<ITarget> WaitForTargetAsync(Func<ITarget, bool> predicate, WaitForOptions options = null)This searches for a target in this specific browser context.
<example>
await page.EvaluateAsync("() => window.open('https://www.example.com/')");var newWindowTarget = await browserContext.WaitForTargetAsync((target) => target.Url == "https://www.example.com/");</example>
Parameters
| Name | Type | Description |
|---|---|---|
predicate |
Func<ITarget, bool> | A function to be run for every target. |
options |
WaitForOptions | options. |
Returns: Task<ITarget> — Resolves to the first target found that matches the predicate function.
Made with love by Dario Kondratiuk