mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
9fbb9f9653
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
62 lines
5.8 KiB
Markdown
62 lines
5.8 KiB
Markdown
---
|
|
sidebar_label: BrowserContext
|
|
---
|
|
|
|
# BrowserContext class
|
|
|
|
[BrowserContext](./puppeteer.browsercontext.md) represents individual user contexts within a [browser](./puppeteer.browser.md).
|
|
|
|
When a [browser](./puppeteer.browser.md) is launched, it has a single [browser context](./puppeteer.browsercontext.md) by default. Others can be created using [Browser.createBrowserContext()](./puppeteer.browser.createbrowsercontext.md). Each context has isolated storage (cookies/localStorage/etc.)
|
|
|
|
[BrowserContext](./puppeteer.browsercontext.md) [emits](./puppeteer.eventemitter.md) various events which are documented in the [BrowserContextEvent](./puppeteer.browsercontextevent.md) enum.
|
|
|
|
If a [page](./puppeteer.page.md) opens another [page](./puppeteer.page.md), e.g. using `window.open`, the popup will belong to the parent [page's browser context](./puppeteer.page.browsercontext.md).
|
|
|
|
#### Signature:
|
|
|
|
```typescript
|
|
export declare abstract class BrowserContext extends EventEmitter<BrowserContextEvents>
|
|
```
|
|
|
|
**Extends:** [EventEmitter](./puppeteer.eventemitter.md)<[BrowserContextEvents](./puppeteer.browsercontextevents.md)>
|
|
|
|
## 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 a new [browser context](./puppeteer.browsercontext.md):
|
|
|
|
```ts
|
|
// Create a new browser context
|
|
const context = await browser.createBrowserContext();
|
|
// 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 | <code>readonly</code> | boolean | Whether this [browser context](./puppeteer.browsercontext.md) is closed. |
|
|
| id | <code>readonly</code> | string \| undefined | Identifier for this [browser context](./puppeteer.browsercontext.md). |
|
|
|
|
## Methods
|
|
|
|
| Method | Modifiers | Description |
|
|
| --------------------------------------------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| [browser()](./puppeteer.browsercontext.browser.md) | | Gets the [browser](./puppeteer.browser.md) associated with this [browser context](./puppeteer.browsercontext.md). |
|
|
| [clearPermissionOverrides()](./puppeteer.browsercontext.clearpermissionoverrides.md) | | Clears all permission overrides for this [browser context](./puppeteer.browsercontext.md). |
|
|
| [close()](./puppeteer.browsercontext.close.md) | | Closes this [browser context](./puppeteer.browsercontext.md) and all associated [pages](./puppeteer.page.md). |
|
|
| [isIncognito()](./puppeteer.browsercontext.isincognito.md) | | <p>Whether this [browser context](./puppeteer.browsercontext.md) is incognito.</p><p>In Chrome, the [default browser context](./puppeteer.browser.defaultbrowsercontext.md) is the only non-incognito browser context.</p> |
|
|
| [newPage()](./puppeteer.browsercontext.newpage.md) | | Creates a new [page](./puppeteer.page.md) in this [browser context](./puppeteer.browsercontext.md). |
|
|
| [overridePermissions(origin, permissions)](./puppeteer.browsercontext.overridepermissions.md) | | Grants this [browser context](./puppeteer.browsercontext.md) the given <code>permissions</code> within the given <code>origin</code>. |
|
|
| [pages()](./puppeteer.browsercontext.pages.md) | | Gets a list of all open [pages](./puppeteer.page.md) inside this [browser context](./puppeteer.browsercontext.md). |
|
|
| [targets()](./puppeteer.browsercontext.targets.md) | | Gets all active [targets](./puppeteer.target.md) inside this [browser context](./puppeteer.browsercontext.md). |
|
|
| [waitForTarget(predicate, options)](./puppeteer.browsercontext.waitfortarget.md) | | <p>Waits until a [target](./puppeteer.target.md) matching the given <code>predicate</code> appears and returns it.</p><p>This will look all open [browser contexts](./puppeteer.browsercontext.md).</p> |
|