Skip to main content
Version: Next

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.
await 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.

debugInfo

readonly

DebugInfo

(Experimental) Get debug information from Puppeteer.

Remarks:

Currently, includes pending protocol calls. In the future, we might add more info.

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.

cookies()

Returns all cookies in the default BrowserContext.

Remarks:

Shortcut for browser.defaultBrowserContext().cookies().

createBrowserContext(options)

Creates a new browser context.

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

defaultBrowserContext()

Gets the default browser context.

Remarks:

The default browser context cannot be closed.

deleteCookie(cookies)

Removes cookies from the default BrowserContext.

Remarks:

Shortcut for browser.defaultBrowserContext().deleteCookie().

disconnect()

Disconnects Puppeteer from this browser, but leaves the process running.

isConnected()

deprecated

Whether Puppeteer is connected to this browser.

Deprecated:

Use Browser.connected.

newPage()

Creates a new page in the default browser context.

pages()

Gets a list of all open pages inside this Browser.

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

Remarks:

Non-visible pages, such as "background_page", will not be listed here. You can find them using Target.page().

process()

Gets the associated ChildProcess.

setCookie(cookies)

Sets cookies in the default BrowserContext.

Remarks:

Shortcut for browser.defaultBrowserContext().setCookie().

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.

Remarks:

The format is always ws://HOST:PORT/devtools/browser/<id>.