October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober 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 Reuse Cookies for Authenticated Puppeteer Screenshots

A practical Puppeteer workflow for saving authorized login cookies, restoring them in the right context, and capturing the authenticated page reliably.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To reuse a website login in Puppeteer, save cookies from the authenticated BrowserContext, restore those cookie objects into the same kind of context before navigation, wait for a site-specific sign that the logged-in page is ready, then call page.screenshot(). Keep the cookie scope and security attributes intact: cookies are credentials, and replaying them does not guarantee the website will accept the session.

Save cookies after an authorized login

This ES module example assumes you have completed the site’s authorized login flow in the page. Replace the example URL and login step with your own application-specific flow.

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';
import { writeFile } from 'node:fs/promises';

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

  await page.goto('https://example.com/login', { waitUntil: 'domcontentloaded' });
  // Complete the site's authorized login steps here.

  const cookies = await context.cookies();
  await writeFile('cookies.json', JSON.stringify(cookies, null, 2), { mode: 0o600 });
} finally {
  await browser.close();
}

context.cookies() returns cookies available to that context. The 0o600 file mode is a practical access restriction on systems that honor Unix-style permissions; it is not a Puppeteer security guarantee. Cookie values are credentials: keep the file out of source control and logs, restrict access, and delete it when it is no longer needed.

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.

Restore cookies before opening the authenticated page

Load the saved cookie objects into a context, create the page from that context, and then navigate. Replace [data-testid="account-home"] with a selector that appears only in the authenticated view of your site.

import puppeteer from 'puppeteer';
import { readFile } from 'node:fs/promises';

const browser = await puppeteer.launch();
try {
  const context = await browser.createBrowserContext();
  const cookies = JSON.parse(await readFile('cookies.json', 'utf8'));
  await context.setCookie(...cookies);

  const page = await context.newPage();
  await page.goto('https://example.com/account', { waitUntil: 'domcontentloaded' });
  await page.waitForSelector('[data-testid="account-home"]');
  await page.screenshot({ path: 'account.png', fullPage: true });
} finally {
  await browser.close();
}

The selector wait is the check that distinguishes a logged-in page from a login redirect or loading shell. domcontentloaded is only a navigation lifecycle milestone; it does not prove that a client-rendered application has finished loading. Puppeteer’s screenshot method returns a Uint8Array by default, or a base64 string when encoding: 'base64' is requested.

Keep cookie scope and context aligned

A BrowserContext isolates browser storage, including cookies and local storage. Restore cookies into the context that owns the page you will capture; another context has a separate cookie jar. The browser-level cookie methods act as shortcuts to the default context, while explicit context methods make the association clear.

Cookie records can include domain, path, expires, httpOnly, sameSite, secure, partitionKey, priority and sourceScheme, in addition to name and value. If expires is omitted, the cookie is a session cookie. Preserve the records as returned rather than reducing them to name/value pairs. Puppeteer’s cookie parameter also supports a url, which can affect the default domain, path and source scheme.

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

Even correctly restored cookies can be rejected: a session may have expired, been revoked or rotated, or be restricted by the site. In those cases, perform a fresh authorized login instead of assuming cookie replay will restore access.

Cookie sessions are not HTTP authentication

Situation Puppeteer API What it does
Website application login represented by browser cookies BrowserContext.cookies() and BrowserContext.setCookie() Reads and restores browser cookie state for pages in that context; the website still decides whether the session is valid.
HTTP authentication page.authenticate() Supplies credentials for HTTP authentication. Puppeteer enables request interception behind the scenes to implement it, which may affect performance.

page.authenticate() is not a general substitute for restoring an application’s cookie-based login session.

Choose a reliable readiness signal

  • Wait for a stable selector present only after authentication, such as an account navigation element.
  • If the application exposes an explicit ready marker, wait for that marker rather than relying only on a navigation event.
  • Before saving a screenshot, verify that the marker belongs to the intended authenticated view and not a shared page shell.

The correct signal is site-specific. A timeout waiting for it may indicate an invalid session, a redirect, a changed selector or an application that has not finished rendering; it should not be treated as proof that the screenshot call itself failed.

Troubleshoot cookie-based captures

  • The screenshot shows a login page: confirm that the saved cookies came from a completed, authorized login; restore them before navigation; and use the same context for the page. If the site invalidated the session, log in again and save a fresh set.
  • setCookie() rejects or fails to apply a cookie: preserve the cookie’s supported scope and attributes from the returned object. Check that its domain or URL association matches the destination and do not discard fields such as secure or same-site settings.
  • The page loads but the selector wait times out: confirm the selector is correct for the authenticated view. Check whether the page redirected, the application uses a different readiness state, or the session was not accepted.
  • Cookies appear missing on a new page: ensure that the page was created from the context into which you restored them. Separate contexts have isolated storage.
  • HTTP credentials do not restore an application session: use cookie restoration for cookie-based application login; reserve page.authenticate() for HTTP authentication.
  • Repeated captures behave inconsistently: sessions may expire or rotate, and sites can impose policies beyond cookie presence. Re-authenticate through an authorized flow and save the new cookie records.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For a URL-based screenshot rather than a Puppeteer session workflow, ScreenshotNeo takes a screenshot or PDF with one GET request. It is a different approach: the API does not take your saved Puppeteer cookie file as input, so use Puppeteer when your target requires a specific authenticated session.

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

See the ScreenshotNeo API documentation for request options. Example cURL request:

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo removes cookie/consent banners, newsletter popups and chat widgets before capture; bot checks, blank pages and failed loads are never billed; its MCP server lets AI agents take screenshots; and the Free plan includes 1,000 screenshots a month with no card, with paid plans starting at $5 for 3,000. Sign up for ScreenshotNeo free.

Frequently Asked Questions

Can I save and restore Puppeteer cookies between browser runs?

Yes. Save the cookie objects returned by the authenticated context, then pass them to that context’s setCookie() before navigating in a later run. The site may still require a fresh login if the session is no longer valid.

Does domcontentloaded mean the authenticated page is ready for a screenshot?

No. It marks a navigation lifecycle point, not completion of client-rendered authenticated content. Wait for a site-specific authenticated-state signal.

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 *

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

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.