ScreenshotNeo

BlogHow-to

How to Get Cookies from a Puppeteer BrowserContext

Use `BrowserContext.cookies()` to read cookies from the Puppeteer context that owns your pages. See runnable examples, scope choices, troubleshooting, and a screenshot alternative.

By the ScreenshotNeo team4 October 20265 min read

Call await context.cookies() on the Puppeteer BrowserContext whose cookies you need. It resolves to a Cookie[]. Choose the context that owns the pages you care about: browser contexts have isolated storage. For the default context, await browser.cookies() is a documented shortcut.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const context = await browser.createBrowserContext();
const page = await context.newPage();

try {
  await page.goto('https://example.com');

  const cookies = await context.cookies();
  console.log(cookies);
} finally {
  await context.close();
  await browser.close();
}

The example creates a non-default context, opens a page in it, navigates, then reads that context’s cookies. Close the context when finished; closing it also closes its associated pages. The default browser context cannot be closed. See the official Puppeteer BrowserContext API, cookies() reference, and cookies guide.

What you have Use What it reads
A specific context await context.cookies() Cookies in that BrowserContext.
The default context await browser.cookies() The default BrowserContext only.
A page, and need its context await page.browserContext().cookies() Cookies in the context that owns that page.

Each BrowserContext represents an individual user context in a browser and has isolated storage. If your program creates multiple contexts, querying the wrong one can return an empty list or cookies unrelated to the page you intended. A page exposes its owning context through page.browserContext().

Runnable examples

Read cookies from a page’s context

import puppeteer from 'puppeteer';

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

  const context = page.browserContext();
  const cookies = await context.cookies();
  console.log(cookies);
} finally {
  await browser.close();
}

browser.newPage() creates a page in the default context. Getting the context from the page makes the scope explicit and also works when the page came from a separately created context.

Read cookies from the default context directly

import puppeteer from 'puppeteer';

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

  const cookies = await browser.cookies();
  console.log(cookies);
} finally {
  await browser.close();
}

browser.cookies() is a shortcut for browser.defaultBrowserContext().cookies(). Use it only when the cookies you want are in the default context.

Filter the returned list in your application

The API returns the context’s cookie array. If you need to select entries, filter the result in JavaScript using the fields available on the returned cookie objects:

const cookies = await context.cookies();
const matching = cookies.filter(cookie => cookie.name === 'session');
console.log(matching);

This filters the retrieved array; it does not change which context Puppeteer queried. Check the official API reference for the current cookie type and its fields rather than assuming application-specific meanings.

Deprecated approach to avoid

page.cookies(urls) is deprecated. The Page API recommends using Browser.cookies() or BrowserContext.cookies() instead. Prefer the context method when scope matters, and the browser shortcut when you specifically want the default context. See the Puppeteer Page API.

Common errors and fixes

Symptom Likely cause Fix
The result is an empty array The selected context has no cookies for the page state you expect, navigation has not completed, or you queried a different context. Check that the page belongs to the context you queried, navigate before reading, and verify the site actually set cookies in that context.
Cookies appear missing when using browser.cookies() The page is in a non-default context. Call context.cookies() on that page’s context, available as page.browserContext().
context is undefined or not the expected object The variable was not assigned from context creation or the page’s browserContext(). Keep the BrowserContext returned by browser.createBrowserContext(), or obtain it from the page, then call cookies().
A page-cookie call warns or fails under newer code The deprecated page.cookies() API is being used. Replace it with await page.browserContext().cookies() or the default-context browser shortcut.
The browser or page is already closed Cookie retrieval ran after the owning context or browser was closed. Read cookies before cleanup, and keep retrieval inside the lifetime of the browser and context.

Reliability, runtime, and handling values

  • Read after the relevant navigation or interaction. A site may set cookies during page activity, so retrieve them after the operation whose resulting state you need.
  • Keep scope explicit. In code that manages several contexts, associate each page with its owning context rather than assuming the default.
  • Await the call. cookies() returns a promise; awaiting it ensures the list is available before you log or process it.
  • Close resources in cleanup. Close non-default contexts when done and close the browser when the job is complete. The finally examples ensure cleanup runs if navigation or retrieval throws.
  • Do not assume cookie values are safe to expose. The API returns cookie data; avoid logging or sharing values unless your application has a clear need and appropriate handling.

The cited API documentation defines the method and return type but does not prescribe how an application should store, disclose, or interpret each cookie field. Treat those decisions as application-specific and consult the current cookie type reference.

Or skip the browser setup

If your goal is a visual record of a page rather than reading its cookie values, ScreenshotNeo captures a website from one request. Its clean-shot flow accepts consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers. Its MCP server includes screenshot, page-info, and PDF tools for AI agents. The free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Every feature is on every plan.

See the ScreenshotNeo documentation. cURL:

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

Python:

import requests

r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://example.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

Sign up for 1,000 free screenshots a month, no card required.

FAQ

What does context.cookies() return?

A promise that resolves to an array of cookies: Promise<Cookie[]>.

Can I use browser.cookies() with an incognito context?

That method is the shortcut for the default context. For a separately created context, call that context’s cookies() method.

Can I get the context from a Page?

Yes. Call page.browserContext(), then call cookies() on the returned context.