October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Set Cookies in Puppeteer

Use the page’s BrowserContext to set cookies before navigation. Learn which fields matter, how the default-context shortcut works, and how to migrate from page.setCookie().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set cookies on the BrowserContext that owns the page, then navigate to the site. In current Puppeteer documentation, Page.setCookie() is obsolete; use page.browserContext().setCookie() for a particular page’s context, or browser.setCookie() when you mean the default context.

Set a cookie before navigating

This example sets a cookie in the context associated with page, then opens the matching site. It uses the documented browser-level cookie data fields; adapt the domain, value, and flags to the site and session you are configuring.

As an Amazon Associate I earn from qualifying purchases.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  try {
    const page = await browser.newPage();
    const context = page.browserContext();

    await context.setCookie({
      name: 'session',
      value: 'example-value',
      domain: 'example.com',
      path: '/',
      httpOnly: true,
      secure: true,
      sameSite: 'Lax',
    });

    await page.goto('https://example.com');
  } finally {
    await browser.close();
  }
})();

The cookie is configured before page.goto(), so it is available to requests made in that context for the matching site scope. The example is documentation-based; the correct cookie scope and flags depend on the target application.

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

Use the default context when that is your target

If the page uses Puppeteer’s default browser context, the browser-level method is a shortcut:

await browser.setCookie({
  name: 'session',
  value: 'example-value',
  domain: 'example.com',
  path: '/',
});

This calls the default context’s cookie-setting method. Do not use this shortcut when the page you intend to configure belongs to a separately created context.

Choose the cookie scope and fields

The current browser-level CookieData interface requires name and value; it also documents domain. Other documented fields let you set expiration, security behavior, and scope. Match these values to the site rather than copying a sample blindly.

  • name and value: the cookie’s key and contents.
  • domain and path: where the cookie applies. Use a domain and path that match the intended site scope.
  • httpOnly: use when the application requires a cookie that client-side scripts cannot read.
  • secure: set to true for a cookie intended for secure transport, such as over HTTPS.
  • sameSite: select a policy that fits the application’s cross-site request requirements; the example uses 'Lax'.
  • expires: an expiration date represented as a number. If omitted, the cookie is a session cookie.
  • url: can affect default domain, path, and source-scheme values.
  • Other documented optional properties include partitioning, priority, and source scheme. Check the interface and browser support for your installed Puppeteer version before relying on browser-specific fields.

Use the context that owns the page

Browser contexts isolate storage, including cookies and local storage. A newly created context does not share cookies or cache with other contexts. Setting a cookie in one context therefore does not configure a page in another.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

For a page-specific setup, obtain its context with page.browserContext() and call setCookie() there. If you create contexts yourself, keep and use the context associated with the page you intend to configure. Use browser.setCookie() only when the default context is the intended one.

Replace obsolete page.setCookie() calls

The current Puppeteer Page API marks page.setCookie() obsolete and directs users to Browser.setCookie() or BrowserContext.setCookie(). For code tied to a particular page, migrate to its context:

// Old pattern: page.setCookie(cookieData)
// Current pattern:
await page.browserContext().setCookie(cookieData);

If the old code deliberately used the default browser context, use await browser.setCookie(cookieData) instead. The key is to preserve the intended context, not merely to change the method name.

Inspect cookies in the default context

The current Browser API documents browser.cookies() as returning cookies in the default BrowserContext:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const cookies = await browser.cookies();
console.log(cookies);

This is useful for checking default-context state. It does not inspect cookies stored in every separately created context.

Or skip the browser setup

If your goal is to capture a page screenshot rather than configure cookies for an automation session, ScreenshotNeo offers a one-request screenshot API. It is not a replacement for Puppeteer cookie setup; it is an option when you need the screenshot itself.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

For example, this cURL request saves a screenshot of Stripe’s site. See the ScreenshotNeo API documentation for request options.

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

ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides screenshot and PDF tools for AI agents. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

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

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

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

Troubleshoot cookie setup

The site does not receive the cookie

  • Confirm you set it on the context that owns the target page. Contexts do not share cookie storage.
  • Check that domain and path match the URL you are visiting.
  • If the cookie is marked secure: true, use the secure scheme expected by the application.
  • Set the cookie before navigation when the initial request needs to include it.

The cookie is missing when you inspect it

Check which context you are inspecting. browser.cookies() reports cookies in the default context, so it will not show cookies held only by a separate context. Inspect or configure the context associated with the page instead.

page.setCookie() is flagged as obsolete

Move the call to page.browserContext().setCookie() for the page’s context, or to browser.setCookie() if the default context is intended. Preserve the original cookie data and verify the context choice.

A cookie option is rejected or has no effect

Confirm the field name, value type, and support in the Puppeteer version and browser you are running. The documented interface includes browser-specific optional fields such as priority and source scheme; do not assume every installed version supports every field.

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

Performance and reliability considerations

Set cookies in the correct context before the navigation that depends on them, and avoid creating a separate context unless you need isolated storage. Context isolation is useful for keeping sessions separate, but it also means cookies and cache are not shared. Cookie acceptance alone does not guarantee that a site will authenticate the session: the value, scope, expiry, and any application-side requirements must also be valid.

The examples show API usage rather than a live-browser validation. For project-specific behavior, check the installed Puppeteer version’s API reference and the target application’s cookie requirements.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.