puppeteer/website/versioned_docs/version-21.7.0/api/puppeteer.browsercontext.md
release-please[bot] 864012a86d
chore: release main (#11593)
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2024-01-04 13:39:46 +00:00

5.6 KiB

sidebar_label
BrowserContext

BrowserContext class

BrowserContext represents individual sessions within a browser.

When a browser is launched, it has a single browser context by default. Others can be created using Browser.createIncognitoBrowserContext().

BrowserContext emits various events which are documented in the BrowserContextEvent enum.

If a page opens another page, e.g. using window.open, the popup will belong to the parent page's browser context.

Signature:

export declare abstract class BrowserContext extends EventEmitter<BrowserContextEvents>

Extends: EventEmitter<BrowserContextEvents>

Remarks

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the BrowserContext class.

Example

Creating an incognito browser context:

// Create a new incognito browser context
const context = await browser.createIncognitoBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();

Properties

Property Modifiers Type Description
closed readonly boolean Whether this browser context is closed.
id readonly string | undefined Identifier for this browser context.

Methods

Method Modifiers Description
browser() Gets the browser associated with this browser context.
clearPermissionOverrides() Clears all permission overrides for this browser context.
close() Closes this browser context and all associated pages.
isIncognito()

Whether this browser context is incognito.

The default browser context is the only non-incognito browser context.

newPage() Creates a new page in this browser context.
overridePermissions(origin, permissions) Grants this browser context the given permissions within the given origin.
pages() Gets a list of all open pages inside this browser context.
targets() Gets all active targets inside this browser context.
waitForTarget(predicate, options)

Waits until a target matching the given predicate appears and returns it.

This will look all open browser contexts.