October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Create an Isolated Browser Context with Puppeteer

Use Puppeteer’s current createBrowserContext() API to separate page storage, open pages in the correct context, and clean up without closing a shared browser.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use await browser.createBrowserContext() to create a fresh Puppeteer browser context, then call await context.newPage() to put pages in it. The context keeps cookies and cache separate from other browser contexts; close it with await context.close() when its work is done.

Runnable example

This ES module example launches a browser, creates a non-default context, opens a page inside it, and cleans up both resources. Install Puppeteer in your project first; the installed package and browser must support the APIs you use.

import puppeteer from 'puppeteer';

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

try {
  // This context has cookies and cache separate from other contexts.
  context = await browser.createBrowserContext();
  const page = await context.newPage();

  await page.goto('https://example.com');
  // Use this page and any popups it opens.
} finally {
  if (context) {
    await context.close();
  }
  await browser.close();
}

The current Puppeteer API reference, version 25.12.0, documents browser.createBrowserContext() as returning a Promise<BrowserContext> and says the new context does not share cookies or cache with other browser contexts: Browser.createBrowserContext().

What the context isolates

A created context separates browser storage from other contexts. Puppeteer’s API overview names cookies and localStorage, and the method reference also specifies cache separation. A page opened in the created context uses that context’s state; a popup opened by that page stays in the same context. In Chrome, non-default contexts are incognito. This describes storage separation within a browser, not a separate browser process or an OS-level security boundary. See the Puppeteer API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put pages in the right context

Use context.newPage() for a page that should use the isolated context. Calling browser.newPage() instead creates a page in the default context, so it will not use the newly created context’s separated storage. The browser API lists these as distinct operations: Puppeteer API reference.

For separate sessions, create one context per session and create each session’s pages from its own context. Popups opened by a page inherit that page’s context; they do not automatically move to the default context.

Close the context you create

await context.close() closes the context and all pages associated with it. The code that creates a context is responsible for its cleanup. If your function borrows a browser instance owned by another part of the application, close only the context it created; do not close the shared browser.

If your code owns the browser lifecycle, await browser.close() closes the browser and its associated pages. browser.disconnect() instead disconnects Puppeteer while leaving the browser process running. These lifecycle methods are documented in Puppeteer’s Browser API reference. The default browser context returned by browser.defaultBrowserContext() cannot be closed: Browser.defaultBrowserContext().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Optional context settings

createBrowserContext() accepts optional BrowserContextOptions. The current reference lists downloadBehavior, proxyBypassList, and proxyServer. The proxy option can specify a proxy for requests; username and password can be set with Page.authenticate(). Check the documentation for your installed Puppeteer version and browser when relying on a specific option’s support or behavior: BrowserContextOptions. None of these options is required to create an isolated context.

Common mistakes and fixes

  • Calling the obsolete method: Use createBrowserContext(), not createIncognitoBrowserContext(). Puppeteer records the rename as a breaking change in version 22.0.0, dated 2024-02-05: Puppeteer changelog.
  • Opening the page on the browser: Replace browser.newPage() with context.newPage() when the page needs the new context’s state.
  • Leaving pages open after a task: Close the created context; doing so closes its associated pages. Close the browser only if your code owns it.
  • Trying to close the default context: The default context cannot be closed. Create a non-default context if you need disposable, separated storage.
  • Expecting a separate process or security sandbox: A browser context provides documented storage isolation, not a separate browser process or a guarantee of process-level security isolation.

Or skip the browser setup

If your goal is a website screenshot rather than browser automation, ScreenshotNeo can return a screenshot with one GET request. Its screenshot API removes cookie and consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. It also provides an MCP server for AI agents. The free plan includes 1,000 screenshots per month with no card, and paid plans start at $5 for 3,000.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Sign up free for 1,000 screenshots a month; no card required.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

How do I keep cookies separate between Puppeteer pages?

Create separate browser contexts and open each page with that context’s newPage() method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Is a Puppeteer browser context a separate browser process?

No. It is a context within the browser; the documented guarantee here concerns storage separation, not a separate process.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.