Skip to main content
Version: 22.13.0

Browser management

Usually, you start working with Puppeteer by either launching launching or connecting to a browser.

Launching a browser

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

const page = await browser.newPage();

// ...

Closing a browser

To gracefully close the browser, you the browser.close() method:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

const page = await browser.newPage();

await browser.close();

Browser contexts

If you need to isolate your automation tasks, use BrowserContexts. Cookies and local storage are not shared between browser contexts. Also, you can close all pages in the context by closing the context.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

const context = await browser.createBrowserContext();

const page1 = await context.newPage();
const page2 = await context.newPage();

await context.close();

Permissions

You can also configure permissions for a browser context:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const context = browser.defaultBrowserContext();

await context.overridePermissions('https://html5demos.com', ['geolocation']);

Connecting to a running browser

If you launched a browser outside of Puppeteer, you can connect to it using connect method. Usually, you can grab a WebSocket endpoint URL from the browser output:

const browser = await puppeteer.connect({
browserWSEndpoint: 'ws://127.0.0.1:9222/...',
});

const page = await browser.newPage();

browser.disconnect();
note

Unlike browser.close(), browser.disconnect() does not shut down the browser or close any pages.