Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Use Puppeteer Browser Contexts for Isolated Sessions

Learn how Puppeteer BrowserContexts separate documented session storage, how to create pages and manage cookies in a context, and how to clean up safely.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use browser.createBrowserContext() to create a separate Puppeteer session, open its pages with context.newPage(), and call context.close() when the session is finished. Puppeteer documents that contexts do not share cookies or cache with other contexts; its BrowserContext reference also describes isolation for storage such as cookies and localStorage. That is storage isolation—not a promise of complete browser-resource or security isolation.

What a browser context isolates

A browser context is a user context within a browser. A newly launched browser has at least one default context; create additional contexts when tasks need separate documented storage state. Puppeteer says cookies and localStorage are isolated between contexts, and the creation method specifically says contexts do not share cookies or cache. See the BrowserContext API reference and createBrowserContext() reference (API documentation identified as version 25.12.0).

As an Amazon Associate I earn from qualifying purchases.

Pages created in the same context are part of that session. A popup opened by one of those pages, such as through window.open(), also belongs to the parent page’s context. Puppeteer’s browser management guide recommends BrowserContexts when automation tasks need isolation.

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

The documentation does not establish that contexts isolate every browser-level resource or provide a security boundary for untrusted tenants. Treat them as a way to separate the documented session storage and cache behavior, not as a substitute for process- or system-level security controls.

Create a context, use it, and close it

This ES module example creates one session, navigates a page in it, and ensures the context and browser are closed even if navigation or other work throws an error. It uses documented API calls composed into a cleanup-safe workflow.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const context = await browser.createBrowserContext();
  try {
    const page = await context.newPage();
    await page.goto('https://example.com');
    // Work with this session's pages and context-scoped state.
  } finally {
    await context.close();
  }
} finally {
  await browser.close();
}
  1. Launch the browser. puppeteer.launch() returns the browser that owns the contexts.
  2. Create a non-default context. Call browser.createBrowserContext(). In Chrome, Puppeteer documents that non-default contexts are incognito. The default context may also be incognito if Chrome was launched with --incognito.
  3. Create pages through the context. Call context.newPage(), rather than browser.newPage(), so the page belongs to the session you intend to isolate.
  4. Do the session’s work. Navigate and interact with the page. Additional pages made with context.newPage() share that context’s session state; popups opened by a page stay in its context.
  5. End the session. Call context.close() to close that context and all its associated pages. Then call browser.close() when the whole browser should shut down.

Manage cookies at the context level

Use the BrowserContext cookie methods when you need to inspect, set, or delete cookies for a session. The context API includes cookies(), setCookie(), and cookie deletion methods. Page-level cookie methods are marked deprecated in the Page API reference, which directs users to browser- or context-level alternatives.

Keep cookie operations tied to the same context as the pages whose session state you intend to manage. A context’s cookies are not shared with other contexts. Consult the current BrowserContext API reference for the exact method signatures and options supported by the Puppeteer version installed in your project.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Choose between a shared context and separate sessions

Need Approach Documented effect
Several pages should work within one session Create them with the same context.newPage() They belong to the same context; popups remain in their parent page’s context.
Separate cookie, localStorage, or cache state across tasks Create a distinct context for each session with browser.createBrowserContext() Contexts do not share cookies or cache; storage such as cookies and localStorage is isolated.
Discard every page in one session together Call context.close() The context and its associated pages close; the browser itself remains open.

These APIs establish the behavior above, but Puppeteer’s cited documentation does not give performance benchmarks for creating a context per task versus reusing pages. Choose based on whether tasks need separate session state and independent teardown; measure your own workload if context creation overhead matters.

Troubleshooting context workflows

  • A page unexpectedly shares session state: Check how it was created. Use context.newPage() on the intended context; pages created from the browser’s default context are not automatically moved into another context.
  • A popup appears in the wrong session: A popup opened by a page belongs to that parent page’s context. Open it from a page already created in the intended context.
  • Other contexts cannot see a cookie: This is expected isolation. Set or inspect the cookie through the context associated with the page that needs it.
  • Closing a context closes pages you still need: context.close() closes all pages associated with it. Keep pages that must remain available in a different context.
  • You cannot close the default context: Puppeteer documents that the default browser context cannot be closed. Close a non-default context to end an isolated session, or close the browser to end the browser process and its contexts.

Or skip the browser setup:

If your goal is simply to capture a website rather than automate an isolated browser session, ScreenshotNeo provides a screenshot API and MCP server. One GET request can return an image or PDF; its clean-shot options can accept consent banners and remove known consent platforms, newsletter popups, and chat widgets before capture. Bot checks, blank pages, and failed loads are not billed, and its MCP server lets AI agents take screenshots.

For example, this cURL request saves a WebP screenshot:

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. It includes 1,000 screenshots per month free with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Can I use a BrowserContext as a security boundary between untrusted users?

No such guarantee is established by the cited Puppeteer documentation. It documents separation of cookies, localStorage, and cache, not complete resource or process isolation.

Does closing a BrowserContext shut down Puppeteer?

No. It closes that context and its associated pages. Close the browser separately with browser.close() when the browser should stop.

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver 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.