Skip to content

BrowserContext

Namespace: PuppeteerSharp

public abstract class BrowserContext : IBrowserContext, IDisposable, IAsyncDisposable

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.

Inheritance: object

Implements: IBrowserContext, IDisposable, IAsyncDisposable

Derived: BidiBrowserContext, CdpBrowserContext

View source

public Browser Browser { get; protected init; }

Value: Browser

View source

public string Id { get; protected init; }

Browser Context Id.

Value: string

View source

public bool IsClosed { get; }

Whether this <a href=“IBrowserContext”>IBrowserContext</a> is closed.

Value: bool

View source

public event EventHandler<TargetChangedArgs> TargetChanged

Raised when the url of a target changes

Value: EventHandler<TargetChangedArgs>

View source

public event EventHandler<TargetChangedArgs> TargetCreated

Raised 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"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Window/open&lt;/a> or NewPageAsync(CreatePageOptions).

Value: EventHandler<TargetChangedArgs>

View source

public event EventHandler<TargetChangedArgs> TargetDestroyed

Raised when a target is destroyed, for example when a page is closed

Value: EventHandler<TargetChangedArgs>

View source

public abstract Task ClearPermissionOverridesAsync()

Clears all permission overrides for the browser context.

Returns: Task — The task.

View source

public abstract Task CloseAsync()

Closes the browser context. All the targets that belong to the browser context will be closed.

Returns: Task — Task.

View source

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.

View source

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.

View source

public void Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View source

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.

View source

public ValueTask DisposeAsync()

Closes the browser context. All the targets that belong to the browser context will be closed.

Returns: ValueTask — ValueTask.

View source

public abstract Task<CookieParam[]> GetCookiesAsync()

Gets all cookies in the browser context.

Returns: Task<CookieParam[]> — Task which resolves to an array of CookieParam.

View source

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.

View source

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”});

View source

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().

View source

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.

View source

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.

View source

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.

View source

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.

View source

Made with love by Dario Kondratiuk