Use browser.cookies() to read cookies and browser.setCookie() to set them in Puppeteer’s default browser context. If your test uses an isolated context, call its context.cookies() and context.setCookie() methods instead. The older page.cookies() and page.setCookie() methods are deprecated; use the browser- or context-level APIs for new code.
Read and set cookies in Puppeteer
This runnable ES module example sets a cookie in the default context, reads the context’s cookies, navigates to the site, then closes the browser. Replace the cookie domain and attributes to match the origin you are testing.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await browser.setCookie({
name: 'session',
value: 'example-value',
domain: 'localhost',
path: '/',
httpOnly: false,
secure: false,
});
console.log(await browser.cookies());
await page.goto('http://localhost');
} finally {
await browser.close();
}
The browser methods operate on the default BrowserContext. Puppeteer’s cookie guide uses this pattern to write and read cookies, and notes that the same operations are available on a BrowserContext. Puppeteer’s cookie guide describes this as useful for storing and restoring specific cookies in tests.
Choose the cookie storage scope
Each browser context has isolated cookies and local storage. Use the methods on the context that owns the pages under test; browser-level methods are convenient shortcuts for the default context.
Recommended Free Tools
#1 Best Overall
| API | Cookie scope | Use it when |
|---|---|---|
browser.cookies() / browser.setCookie() |
The default BrowserContext. | Your pages use the default context and share its cookie state. |
context.cookies() / context.setCookie() |
The particular BrowserContext object. | A test needs isolated state, or you run multiple contexts that should not share cookies. |
For a separate context, set up, inspect, and clean up cookies through that same object:
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const context = await browser.createBrowserContext();
const page = await context.newPage();
await context.setCookie({
name: 'session',
value: 'example-value',
domain: 'localhost',
path: '/',
httpOnly: false,
secure: false,
});
console.log(await context.cookies());
await page.goto('http://localhost');
} finally {
await browser.close();
}
See the Browser.cookies() reference, Browser.setCookie() reference, and BrowserContext.setCookie() reference.
Rank #2
Set a cookie before navigation
Call setCookie() before page.goto() when the page should receive that cookie on its initial request. The cookie’s domain and security attributes must suit the site under test; do not assume a localhost cookie will apply to another origin. Create the page in the same context whose cookies you set.
- Launch Puppeteer and choose the default context or create a separate one.
- Create a page in that context.
- Set the cookie on the browser or context, with a domain and path appropriate for the target site.
- Navigate to the matching site and verify state with the cookie-reading method on the same scope.
Choose the cookie attributes
The CookieData interface requires name, value, and domain. Optional properties include:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →expires: an expiration value; when expiry is omitted, the interface describes the cookie as a session cookie.httpOnly: a boolean attribute indicating whether the cookie is HTTP-only.path: the cookie path, commonly/when it should cover the site’s paths.secure: a boolean security attribute; set it to suit the scheme and behavior of the site under test.sameSite: the cookie’s SameSite setting, using the values documented by Puppeteer’s cookie type.partitionKey: identifies the site or origin context for a partitioned cookie. Support and descriptions differ between Chrome and Firefox, so check the reference for the browser you use.
These attributes are browser cookie semantics, not a guarantee that every browser handles every option identically. Consult the CookieData API reference when using less common fields.
Read back or remove cookies during test cleanup
Use the corresponding cookies() method to inspect the cookie state after setup. Keep the context consistent: reading from the default browser context will not inspect cookies stored in a separate context. Puppeteer’s cookie guide also demonstrates deleting cookies for cleanup; remove test state when your test design reuses a context rather than relying on unrelated pages to clean it up.
Rank #4
Why is page.setCookie() deprecated?
Puppeteer marks Page.setCookie() obsolete and directs users to Browser.setCookie() or BrowserContext.setCookie(). The page-level cookie reader is likewise deprecated in favor of browser- or context-level cookie methods. Update new code to make the storage owner explicit, especially when tests use isolated contexts.
The legacy Page.setCookie() and Page.cookies() references remain useful when migrating older code. The legacy CookieParam interface documents a url property for the page-level API; it can affect default domain, path, and source-scheme values. Treat that as legacy API context rather than a reason to use the deprecated Page methods in new code.
Best Value
- Used Book in Good Condition
Common cookie setup problems
- The cookie does not appear after navigation: confirm that the cookie domain matches the site and that the page and cookie use the same browser context.
- The test reads an empty or unexpected cookie list: inspect cookies from the context that owns the page.
browser.cookies()reads the default context, not a different isolated context. - A secure cookie is not behaving as expected: check the target scheme and the cookie’s
securesetting. Cookie options are subject to browser behavior; do not assume identical support across browsers. - New code triggers deprecation guidance: replace Page cookie methods with
browser.cookies()/browser.setCookie()for the default context or the matchingcontextmethods for an isolated context.
Or skip the browser setup
If your goal is a clean website screenshot rather than manipulating cookies in a Puppeteer test, ScreenshotNeo offers a one-request screenshot API. See the API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
- Cookie and consent banners are accepted before capture; 60+ known consent platforms, newsletter popups, and chat widgets can be removed, with each step configurable.
- Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing; response headers report the page verdict and billing status.
- An MCP server provides
take_screenshot,get_page_info, andcapture_pdftools for AI agents and MCP clients. - The Free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000 screenshots.
Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.
Quick Recap
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.

