puppeteer/website/versioned_docs/version-21.3.0/api/puppeteer.browser.md
release-please[bot] 04384aeaec
chore: release main (#10907)
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2023-09-19 13:03:19 +00:00

12 KiB

sidebar_label
Browser

Browser class

Browser represents a browser instance that is either:

Browser emits various events which are documented in the BrowserEvent enum.

Signature:

export declare abstract class Browser extends EventEmitter<BrowserEvents>

Extends: EventEmitter<BrowserEvents>

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 Browser class.

Example 1

Using a Browser to create a Page:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();

Example 2

Disconnecting from and reconnecting to a Browser:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
// Store the endpoint to be able to reconnect to the browser.
const browserWSEndpoint = browser.wsEndpoint();
// Disconnect puppeteer from the browser.
browser.disconnect();

// Use the endpoint to reestablish a connection
const browser2 = await puppeteer.connect({browserWSEndpoint});
// Close the browser.
await browser2.close();

Properties

Property Modifiers Type Description
connected readonly boolean Whether Puppeteer is connected to this browser.

Methods

Method Modifiers Description
browserContexts()

Gets a list of open browser contexts.

In a newly-created browser, this will return a single instance of BrowserContext.

close() Closes this browser and all associated pages.
createIncognitoBrowserContext(options)

Creates a new incognito browser context.

This won't share cookies/cache with other browser contexts.

defaultBrowserContext() Gets the default browser context.
disconnect() Disconnects Puppeteer from this browser, but leaves the process running.
isConnected() Whether Puppeteer is connected to this browser.
newPage() Creates a new page in the default browser context.
pages()

Gets a list of all open pages inside this .

If there ar multiple browser contexts, this returns all pages in all browser contexts.

process() Gets the associated ChildProcess.
target() Gets the target associated with the default browser context).
targets()

Gets all active targets.

In case of multiple browser contexts, this returns all targets in all browser contexts.

userAgent()

Gets this browser's original user agent.

Pages can override the user agent with Page.setUserAgent().

version()

Gets a string representing this browser's name and version.

For headless browser, this is similar to "HeadlessChrome/61.0.3153.0". For non-headless or new-headless, this is similar to "Chrome/61.0.3153.0". For Firefox, it is similar to "Firefox/116.0a1".

The format of Browser.version() might change with future releases of browsers.

waitForTarget(predicate, options)

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

This will look all open browser contexts.

wsEndpoint()

Gets the WebSocket URL to connect to this browser.

This is usually used with Puppeteer.connect().

You can find the debugger URL (webSocketDebuggerUrl) from http://${host}:${port}/json/version.

See browser endpoint for more information.