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.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
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();

Hierarchy

  • EventEmitter<BrowserEvents>
    • Browser

Constructors

  • Returns Browser

Accessors

  • get connected(): boolean
  • Whether Puppeteer is connected to this browser.

    Returns boolean

  • get debugInfo(): DebugInfo
  • Experimental

    Get debug information from Puppeteer.

    Returns DebugInfo

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

Methods

  • Gets a list of open BrowserContext | browser contexts.

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

    Returns BrowserContext[]

  • Closes this browser and all associated pages.

    Returns Promise<void>

  • Creates a new BrowserContext | browser context.

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

    Parameters

    • Optionaloptions: BrowserContextOptions

    Returns Promise<BrowserContext>

    import puppeteer from 'puppeteer';

    const browser = await puppeteer.launch();
    // Create a new browser context.
    const context = await browser.createBrowserContext();
    // Create a new page in a pristine context.
    const page = await context.newPage();
    // Do stuff
    await page.goto('https://example.com');
  • Gets the default BrowserContext | browser context.

    Returns BrowserContext

    The default BrowserContext | browser context cannot be closed.

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

    Returns Promise<void>

  • Emit an event and call any associated listeners.

    Type Parameters

    • Key extends keyof BrowserEvents

    Parameters

    • type: Key

      the event you'd like to emit

    • event: EventsWithWildcard<BrowserEvents>[Key]

    Returns boolean

    true if there are any listeners, false if there are not.

  • Whether Puppeteer is connected to this browser.

    Returns boolean

    Use Browser.connected.

  • Gets the number of listeners for a given event.

    Parameters

    • type: keyof BrowserEvents

      the event to get the listener count for

    Returns number

    the number of listeners bound to the given event

  • Remove an event listener from firing.

    Type Parameters

    • Key extends keyof BrowserEvents

    Parameters

    • type: Key

      the event type you'd like to stop listening to.

    • Optionalhandler: Handler<EventsWithWildcard<BrowserEvents>[Key]>

      the function that should be removed.

    Returns this

    this to enable you to chain method calls.

  • Bind an event listener to fire when an event occurs.

    Type Parameters

    • Key extends keyof BrowserEvents

    Parameters

    • type: Key

      the event type you'd like to listen to. Can be a string or symbol.

    • handler: Handler<EventsWithWildcard<BrowserEvents>[Key]>

      the function to be called when the event occurs.

    Returns this

    this to enable you to chain method calls.

  • Like on but the listener will only be fired once and then it will be removed.

    Type Parameters

    • Key extends keyof BrowserEvents

    Parameters

    • type: Key

      the event you'd like to listen to

    • handler: Handler<EventsWithWildcard<BrowserEvents>[Key]>

      the handler function to run when the event occurs

    Returns this

    this to enable you to chain method calls.

  • Gets a list of all open pages inside this Browser.

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

    Returns Promise<Page[]>

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

  • Gets the associated ChildProcess.

    Returns null | ChildProcess

    null if this instance was connected to via Puppeteer.connect.

  • Removes all listeners. If given an event argument, it will remove only listeners for that event.

    Parameters

    • Optionaltype: keyof BrowserEvents

      the event to remove listeners for.

    Returns this

    this to enable you to chain method calls.

  • Gets the Target | target associated with the default browser context).

    Returns Target

  • Gets all active Target | targets.

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

    Returns Target[]

  • Gets this browser's original user agent.

    Pages can override the user agent with Page.setUserAgent.

    Returns Promise<string>

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

    Returns Promise<string>

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

    This will look all open BrowserContext | browser contexts.

    Parameters

    • predicate: ((x: Target) => boolean | Promise<boolean>)
        • (x): boolean | Promise<boolean>
        • Parameters

          • x: Target

          Returns boolean | Promise<boolean>

    • Optionaloptions: WaitForTargetOptions

    Returns Promise<Target>

    await page.evaluate(() => window.open('https://www.example.com/'));
    const newWindowTarget = await browser.waitForTarget(
    target => target.url() === 'https://www.example.com/'
    );
  • 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.

    Returns string

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