Interface IBrowserContext
- Namespace
- PuppeteerSharp
- Assembly
- PuppeteerSharp.dll
BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single IBrowserContext used by default. The method NewPageAsync() creates a IPage in the default IBrowserContext.
public interface IBrowserContext
Properties
Browser
Gets the browser this browser context belongs to.
IBrowser Browser { get; }
Property Value
Id
Browser Context Id.
string Id { get; }
Property Value
IsClosed
Whether this IBrowserContext is closed.
bool IsClosed { get; }
Property Value
IsIncognito
Returns whether BrowserContext is incognito The default browser context is the only non-incognito browser context.
[Obsolete("In Chrome, the default browser context can also be \"icognito\" if configured via the arguments and in such cases this getter returns wrong results. Also, the term \"incognito\" is not applicable to other browsers. To migrate, check the default browser context instead: in Chrome all non-default contexts are incognito, and the default context might be incognito if you provide the `--incognito` argument when launching the browser.")]
bool IsIncognito { get; }
Property Value
Remarks
The default browser context cannot be closed.
Methods
ClearPermissionOverridesAsync()
Clears all permission overrides for the browser context.
Task ClearPermissionOverridesAsync()
Returns
- Task
The task.
CloseAsync()
Closes the browser context. All the targets that belong to the browser context will be closed.
Task CloseAsync()
Returns
- Task
Task.
NewPageAsync()
Creates a new page.
Task<IPage> NewPageAsync()
Returns
OverridePermissionsAsync(string, IEnumerable<OverridePermission>)
Overrides the browser context permissions.
Task OverridePermissionsAsync(string origin, IEnumerable<OverridePermission> permissions)
Parameters
origin
stringThe 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.
Examples
var context = browser.DefaultBrowserContext; await context.OverridePermissionsAsync("https://html5demos.com", new List<string> {"geolocation"});
- See Also
PagesAsync()
An array of all pages inside the browser context.
Task<IPage[]> PagesAsync()
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().
Targets()
Gets an array of all active targets inside the browser context.
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/");
Task<ITarget> WaitForTargetAsync(Func<ITarget, bool> predicate, WaitForOptions options = null)
Parameters
predicate
Func<ITarget, bool>A function to be run for every target.
options
WaitForOptionsoptions.
Returns
Events
TargetChanged
Raised when the url of a target changes
event EventHandler<TargetChangedArgs> TargetChanged
Event Type
TargetCreated
Raised when a target is created, for example when a new page is opened by window.open
https://developer.mozilla.org/en-US/docs/Web/API/Window/open or NewPageAsync().
event EventHandler<TargetChangedArgs> TargetCreated
Event Type
TargetDestroyed
Raised when a target is destroyed, for example when a page is closed
event EventHandler<TargetChangedArgs> TargetDestroyed