Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/pptr/src/Browser.ts>Browser

Headless Chrome Deno API
Latest
class Browser
extends EventEmitter
import { Browser } from "https://deno.land/x/pptr@1.2.0/src/Browser.ts";

A Browser is created when Puppeteer connects to a Chromium instance, either through PuppeteerNode.launch or Puppeteer.connect.

Examples

An example of using a Browser to create a Page:

const puppeteer = require('puppeteer');

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

An example of disconnecting from and reconnecting to a Browser:

const puppeteer = require('puppeteer');

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

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

Constructors

new
Browser(
connection: Connection,
contextIds: string[],
ignoreHTTPSErrors: boolean,
defaultViewport?: Viewport | null,
process?: Deno.Process,
closeCallback?: BrowserCloseCallback,
)

Properties

private
_closeCallback: BrowserCloseCallback
private
_connection: Connection
private
_contexts: Map<string, BrowserContext>
private
_defaultContext: BrowserContext
private
optional
_defaultViewport: Viewport | null
private
_ignoreHTTPSErrors: boolean
private
optional
_process: Deno.Process
_targets: Map<string, Target>

Methods

private
_getVersion(): Promise<Protocol.Browser.GetVersionResponse>
private
_targetCreated(event: Protocol.Target.TargetCreatedEvent): Promise<void>
private
_targetDestroyed(event: { targetId: string; }): Promise<void>
private
_targetInfoChanged(event: Protocol.Target.TargetInfoChangedEvent): void
_createPageInContext(contextId?: string): Promise<Page>
_disposeContext(contextId?: string): Promise<void>

Returns an array of all open browser contexts. In a newly created browser, this will return a single instance of BrowserContext.

close(): Promise<void>

Closes Chromium and all of its pages (if any were opened). The Browser object itself is considered to be disposed and cannot be used anymore.

Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.

Returns the default browser context. The default browser context cannot be closed.

disconnect(): void

Disconnects Puppeteer from the browser, but leaves the Chromium process running. After calling disconnect, the Browser object is considered disposed and cannot be used anymore.

isConnected(): boolean

Indicates that the browser is connected.

newPage(): Promise<Page>

Creates a Page in the default browser context.

pages(): Promise<Page[]>

An array of all open pages inside the Browser.

process(): Deno.Process | null

The spawned browser process. Returns null if the browser instance was created with Puppeteer.connect.

The target associated with the browser.

All active targets inside the Browser. In case of multiple browser contexts, returns an array with all the targets in all browser contexts.

userAgent(): Promise<string>

The browser's original user agent. Pages can override the browser user agent with Page.setUserAgent.

version(): Promise<string>

A string representing the browser name and version.

waitForTarget(predicate: (x: Target) => boolean, options?: WaitForTargetOptions): Promise<Target>

Searches for a target in all browser contexts.

wsEndpoint(): string

The browser websocket endpoint which can be used as an argument to Puppeteer.connect.

Static Methods

create(
connection: Connection,
contextIds: string[],
ignoreHTTPSErrors: boolean,
defaultViewport?: Viewport | null,
process?: Deno.Process,
closeCallback?: BrowserCloseCallback,
): Promise<Browser>