2022-07-05 13:41:43 +00:00
---
sidebar_label: Browser
---
# Browser class
2023-09-18 09:05:23 +00:00
[Browser ](./puppeteer.browser.md ) represents a browser instance that is either:
- connected to via [Puppeteer.connect() ](./puppeteer.puppeteer.connect.md ) or - launched by [PuppeteerNode.launch() ](./puppeteer.puppeteernode.launch.md ).
2024-04-11 11:01:13 +00:00
[Browser ](./puppeteer.browser.md ) [emits ](./puppeteer.eventemitter.emit.md ) various events which are documented in the [BrowserEvent ](./puppeteer.browserevent.md ) enum.
2022-07-05 13:41:43 +00:00
2022-10-24 07:07:05 +00:00
#### Signature:
2022-07-05 13:41:43 +00:00
```typescript
2023-09-18 09:05:23 +00:00
export declare abstract class Browser extends EventEmitter< BrowserEvents >
2022-07-05 13:41:43 +00:00
```
2023-09-13 13:47:55 +00:00
**Extends:** [EventEmitter ](./puppeteer.eventemitter.md )< [BrowserEvents](./puppeteer.browserevents.md)>
2022-07-05 13:41:43 +00:00
## 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
2023-09-18 09:05:23 +00:00
Using a [Browser ](./puppeteer.browser.md ) to create a [Page ](./puppeteer.page.md ):
2022-07-05 13:41:43 +00:00
```ts
2022-12-09 12:57:39 +00:00
import puppeteer from 'puppeteer';
2022-07-05 13:41:43 +00:00
2023-09-18 09:05:23 +00:00
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
2022-07-05 13:41:43 +00:00
```
## Example 2
2023-09-18 09:05:23 +00:00
Disconnecting from and reconnecting to a [Browser ](./puppeteer.browser.md ):
2022-07-05 13:41:43 +00:00
```ts
2022-12-09 12:57:39 +00:00
import puppeteer from 'puppeteer';
2022-07-05 13:41:43 +00:00
2023-09-18 09:05:23 +00:00
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.
2023-11-30 14:27:49 +00:00
await browser.disconnect();
2023-09-18 09:05:23 +00:00
// Use the endpoint to reestablish a connection
const browser2 = await puppeteer.connect({browserWSEndpoint});
// Close the browser.
await browser2.close();
2022-07-05 13:41:43 +00:00
```
2023-09-18 09:14:10 +00:00
## Properties
2024-03-20 15:03:14 +00:00
< table > < thead > < tr > < th >
Property
< / th > < th >
Modifiers
< / th > < th >
Type
< / th > < th >
Description
< / th > < / tr > < / thead >
< tbody > < tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "connected" > connected< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
`readonly`
< / td > < td >
boolean
< / td > < td >
Whether Puppeteer is connected to this [browser ](./puppeteer.browser.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "debuginfo" > debugInfo< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
`readonly`
< / td > < td >
[DebugInfo ](./puppeteer.debuginfo.md )
< / td > < td >
2024-04-29 12:50:39 +00:00
**_(Experimental)_** Get debug information from Puppeteer.
**Remarks:**
Currently, includes pending protocol calls. In the future, we might add more info.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< / tbody > < / table >
2023-09-18 09:14:10 +00:00
2022-07-05 13:41:43 +00:00
## Methods
2024-03-20 15:03:14 +00:00
< table > < thead > < tr > < th >
Method
< / th > < th >
Modifiers
< / th > < th >
Description
< / th > < / tr > < / thead >
< tbody > < tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "browsercontexts" > [browserContexts()](./puppeteer.browser.browsercontexts.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets a list of open [browser contexts ](./puppeteer.browsercontext.md ).
In a newly-created [browser ](./puppeteer.browser.md ), this will return a single instance of [BrowserContext ](./puppeteer.browsercontext.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "close" > [close()](./puppeteer.browser.close.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Closes this [browser ](./puppeteer.browser.md ) and all associated [pages ](./puppeteer.page.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "createbrowsercontext" > [createBrowserContext(options)](./puppeteer.browser.createbrowsercontext.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Creates a new [browser context ](./puppeteer.browsercontext.md ).
This won't share cookies/cache with other [browser contexts ](./puppeteer.browsercontext.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "defaultbrowsercontext" > [defaultBrowserContext()](./puppeteer.browser.defaultbrowsercontext.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets the default [browser context ](./puppeteer.browsercontext.md ).
2024-04-29 12:50:39 +00:00
**Remarks:**
The default [browser context ](./puppeteer.browsercontext.md ) cannot be closed.
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "disconnect" > [disconnect()](./puppeteer.browser.disconnect.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Disconnects Puppeteer from this [browser ](./puppeteer.browser.md ), but leaves the process running.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "isconnected" > [isConnected()](./puppeteer.browser.isconnected.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
2024-04-04 07:17:26 +00:00
`deprecated`
2024-03-20 15:03:14 +00:00
< / td > < td >
Whether Puppeteer is connected to this [browser ](./puppeteer.browser.md ).
2024-04-04 07:17:26 +00:00
**Deprecated:**
Use [Browser.connected ](./puppeteer.browser.md ).
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "newpage" > [newPage()](./puppeteer.browser.newpage.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Creates a new [page ](./puppeteer.page.md ) in the [default browser context ](./puppeteer.browser.defaultbrowsercontext.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "pages" > [pages()](./puppeteer.browser.pages.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets a list of all open [pages ](./puppeteer.page.md ) inside this [Browser ](./puppeteer.browser.md ).
If there ar multiple [browser contexts ](./puppeteer.browsercontext.md ), this returns all [pages ](./puppeteer.page.md ) in all [browser contexts ](./puppeteer.browsercontext.md ).
2024-04-29 12:50:39 +00:00
**Remarks:**
Non-visible [pages ](./puppeteer.page.md ), such as `"background_page"` , will not be listed here. You can find them using [Target.page() ](./puppeteer.target.page.md ).
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "process" > [process()](./puppeteer.browser.process.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets the associated [ChildProcess ](https://nodejs.org/api/child_process.html#class-childprocess ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "target" > [target()](./puppeteer.browser.target.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets the [target ](./puppeteer.target.md ) associated with the [default browser context ](./puppeteer.browser.defaultbrowsercontext.md )).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "targets" > [targets()](./puppeteer.browser.targets.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets all active [targets ](./puppeteer.target.md ).
In case of multiple [browser contexts ](./puppeteer.browsercontext.md ), this returns all [targets ](./puppeteer.target.md ) in all [browser contexts ](./puppeteer.browsercontext.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "useragent" > [userAgent()](./puppeteer.browser.useragent.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets this [browser's ](./puppeteer.browser.md ) original user agent.
[Pages ](./puppeteer.page.md ) can override the user agent with [Page.setUserAgent() ](./puppeteer.page.setuseragent.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "version" > [version()](./puppeteer.browser.version.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets a string representing this [browser's ](./puppeteer.browser.md ) 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() ](./puppeteer.browser.version.md ) might change with future releases of browsers.
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "waitfortarget" > [waitForTarget(predicate, options)](./puppeteer.browser.waitfortarget.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Waits until a [target ](./puppeteer.target.md ) matching the given `predicate` appears and returns it.
This will look all open [browser contexts ](./puppeteer.browsercontext.md ).
< / td > < / tr >
< tr > < td >
2024-04-11 11:01:13 +00:00
< span id = "wsendpoint" > [wsEndpoint()](./puppeteer.browser.wsendpoint.md)< / span >
2024-03-20 15:03:14 +00:00
< / td > < td >
< / td > < td >
Gets the WebSocket URL to connect to this [browser ](./puppeteer.browser.md ).
This is usually used with [Puppeteer.connect() ](./puppeteer.puppeteer.connect.md ).
You can find the debugger URL (`webSocketDebuggerUrl`) from `http://HOST:PORT/json/version` .
See [browser endpoint ](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target ) for more information.
2024-04-29 12:50:39 +00:00
**Remarks:**
The format is always `ws://HOST:PORT/devtools/browser/<id>` .
2024-03-20 15:03:14 +00:00
< / td > < / tr >
< / tbody > < / table >