DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon 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 desk5 min

How to Use Cookies with Puppeteer: Set, Read, and Delete Them

Use Puppeteer’s BrowserContext cookie methods to set cookies before navigation, read them from the correct storage context, and delete them with current APIs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Puppeteer’s BrowserContext cookie methods when you want to control cookies for a particular page and its context: call context.setCookie() before navigation, context.cookies() to read cookies, and a context deletion method to remove them. For the default context, browser.cookies(), browser.setCookie(), and browser.deleteCookie() are convenient alternatives. The right choice depends on which context owns the page and its storage.

Set a cookie before navigating

Create the page in the context whose cookie storage you want to use, set the cookie on that context, and then navigate. The following example uses the documented context-level API:

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';

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

try {
  const page = await context.newPage();

  await context.setCookie({
    name: 'session_hint',
    value: 'example',
    url: 'https://example.com',
    secure: true,
    httpOnly: true,
    sameSite: 'Lax',
  });

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

  const cookies = await context.cookies();
  console.log(cookies);

  await context.deleteMatchingCookies({ name: 'session_hint' });
} finally {
  await context.close();
  await browser.close();
}

This example illustrates the documented interface; it is not a claim that the code was executed. Check the types and documentation for your installed Puppeteer version before relying on optional fields or filter shapes. The official guide also shows the default-context pattern, using browser.cookies() to retrieve cookies and browser.setCookie() and browser.deleteCookie() to write and remove them. Puppeteer’s cookies guide includes an example with explicit domain, path, expiry, and security-related values.

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

Choose the context that owns the page

A BrowserContext isolates browser storage, including cookies and localStorage. Pages opened in that context, including popups, belong to it. A cookie set in one context should not be assumed to be available to a page in another. Use the browser-level cookie methods for default-context work; use the corresponding BrowserContext methods when you need to work with a specific context’s state. See the BrowserContext API reference.

  • Default context: use browser.cookies(), browser.setCookie(), or browser.deleteCookie() when the target page uses the default context.
  • Explicit context: create or select the context that owns the target page, then use its cookie methods. This is useful when you need to keep that page’s storage separate from another context’s.

Set the cookie’s scope and attributes deliberately

The values you provide determine where the cookie applies and how it behaves. The documented CookieData fields include name, value, domain, expires, httpOnly, partitionKey, path, priority, sameSite, secure, and sourceScheme. The CookieParam interface also documents url, which can affect default domain, path, and source scheme.

  • url versus domain and path: these are not interchangeable. A URL can inform defaults; explicit domain and path values specify the intended scope. Use the host and path that should receive the cookie.
  • secure and sameSite: set them to match the cookie’s intended security and cross-site behavior. Do not treat them as cosmetic fields.
  • httpOnly: this is a cookie attribute. Do not assume every cookie can be read or changed through page JavaScript such as document.cookie.
  • partitionKey: consider it when working with a partitioned cookie; omitting relevant partition information can mean you are not addressing the cookie you intend.
  • Chrome-supported fields: the interface descriptions mark priority and sourceScheme as Chrome-supported. Verify that they fit your browser and installed Puppeteer version.

The interface references establish the available fields, not that every site or browser configuration will accept every cookie. A scope or attribute mismatch can prevent the cookie from being sent where you expect.

Read and verify cookies

Use the cookie-reading method on the same storage owner where you set the cookie: context.cookies() for a specific context or browser.cookies() for the default context. Inspect the returned cookie data to check its name and scope-related values. If it is absent, first verify that you are reading from the page’s actual context and that the cookie’s URL, domain, path, and partition information correspond to the intended site.

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

Delete cookies with current APIs

For a context-scoped cleanup, context.deleteMatchingCookies() removes cookies matching the filters you provide. Use it when you want to target a cookie by a filter such as its name. The BrowserContext.deleteMatchingCookies() reference documents the method and its filters.

Puppeteer marks Page.deleteCookie() obsolete. Prefer deletion through Browser or BrowserContext, including matching-cookie deletion where appropriate, rather than building new code around the obsolete page method. See the Page.deleteCookie() reference for that deprecation guidance.

Troubleshoot cookies that do not appear or apply

  • The page does not receive the cookie: confirm you set it on the same context that owns the page, and set it before navigation if the initial request needs it. Review the URL or explicit domain and path, plus secure and SameSite intent.
  • Cookie reads return unexpected results: read from the owning context, not a different context or the default browser context by assumption.
  • A cookie cannot be inspected with document.cookie: Puppeteer’s cookie interfaces include the httpOnly attribute. Use Puppeteer’s browser or context cookie methods for browser storage rather than assuming page JavaScript can expose every cookie.
  • A field or deletion filter is rejected: check the exact types and signatures for the Puppeteer version installed in your project. The official reference results identify CookieData and BrowserContext at 25.12.0 and CookieParam at 25.11.0; those version labels do not establish that every project uses those versions.
  • Deletion does not remove the intended cookie: ensure the deletion call targets the context that owns it and that the matching filter describes the cookie you want removed.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is a website screenshot rather than controlling browser storage in Puppeteer, ScreenshotNeo is a screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. For example, this cURL call saves a WebP screenshot of Stripe; see the ScreenshotNeo API documentation for request options and setup.

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 or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and each response identifies the page verdict and billing status in headers. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Sign up for 1,000 free screenshots a month with no card.

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

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 *

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.

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
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.