Co-authored-by: Maksim Sadym <sadym@google.com>
12 KiB
sidebar_label |
---|
Browser |
Browser class
Browser represents a browser instance that is either:
- connected to via Puppeteer.connect() or - launched by PuppeteerNode.launch().
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. |
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 Browser. 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 The format of Browser.version() might change with future releases of browsers. |
|
waitForTarget(predicate, options) | Waits until a target matching the given 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 ( See browser endpoint for more information. |