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.

To keep a site’s consent choice between separate Puppeteer runs, launch the browser with the same persistent userDataDir each time. If you need a fresh or isolated browser context, save the site’s actual consent state and restore it before navigating to the page. First determine whether that site stores consent in cookies, localStorage, or another mechanism—there is no universal consent cookie name.

Choose the right persistence method

Method What persists Best fit Trade-off
Reuse userDataDir The browser profile’s stored state Returning-user screenshots across separate launches Runs share profile data; secure the directory and avoid concurrent browser processes using it.
Save and restore consent state Only the cookies or other storage you select Fresh contexts or repeatable test setup You must preserve relevant cookie metadata and handle non-cookie storage if the site uses it.

Puppeteer documents userDataDir as a launch option for selecting a user data directory. Browser contexts, by contrast, isolate storage such as cookies and localStorage; a new context does not inherit consent from another one. See the LaunchOptions reference and BrowserContext reference.

Option 1: Reuse a profile across launches

Use one stable profile path for runs that should represent the same browser user. The following example uses Puppeteer’s default Chromium launch and captures a screenshot after navigation.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: true,
    userDataDir: './puppeteer-profile'
  });

  try {
    const page = await browser.newPage();
    await page.goto('https://example.com', { waitUntil: 'networkidle2' });
    await page.screenshot({ path: 'shot.png', fullPage: true });
  } finally {
    await browser.close();
  }
})();

Run the script again with the same working directory and profile path. After you accept the site’s consent prompt during a run, the profile can retain the site’s stored state for the next launch. Do not delete or change the profile directory between runs. Since a profile may contain cookies and other browsing data, restrict access to it. Do not start multiple browser processes against the same profile at once.

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

Option 2: Restore selected cookies in a fresh context

For isolated runs, read cookies after consent has been accepted, save the relevant records securely, and set them on the new context before opening the target page. Context-level APIs keep this operation scoped to the context you intend to use.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: true });
  try {
    const context = await browser.createBrowserContext();

    // Replace these with the site's actual consent cookies, captured
    // after accepting consent and stored securely between runs.
    const savedConsentCookies = [
      {
        name: 'replace-with-actual-cookie-name',
        value: 'replace-with-actual-cookie-value',
        domain: 'example.com',
        path: '/',
        secure: true,
        httpOnly: false,
        sameSite: 'Lax'
      }
    ];

    await context.setCookie(...savedConsentCookies);
    const page = await context.newPage();
    await page.goto('https://example.com', { waitUntil: 'networkidle2' });
    await page.screenshot({ path: 'shot.png', fullPage: true });
  } finally {
    await browser.close();
  }
})();

The cookie object above is illustrative, not a real consent cookie for example.com. Replace it with records obtained from the site; do not copy the sample name or value. Puppeteer supports reading and setting cookies on browser contexts, and cookie attributes can include domain, path, expiration, secure, httpOnly, and sameSite. Preserve the attributes relevant to the cookie’s behavior. An omitted expiration represents a session cookie, so it does not have the same lifetime as a persistent cookie. See Puppeteer’s cookie guide, BrowserContext.cookies(), and BrowserContext.setCookie() references.

For a default browser context, the corresponding APIs are available on Browser. Puppeteer’s page-level cookie methods are deprecated in favor of browser- or context-level methods. See the Browser.cookies() reference and Page.setCookie() deprecation note.

Check for localStorage or another consent mechanism

Some consent managers do not rely on cookies alone. Inspect the site after accepting consent, using the browser’s developer tools or page code, to identify what changes and where it is stored. Puppeteer documents that localStorage is isolated by browser context, but the general API documentation does not identify a particular website’s consent key or provide a universal export format.

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

If the site stores consent in localStorage, capture the relevant state and restore it in the page before the site’s scripts check for consent. One approach is to register an initialization script on the new page before navigating:

const consentStorage = {
  // Replace with the actual key and value observed on your target site.
  'replace-with-actual-consent-key': 'replace-with-actual-consent-value'
};

await page.evaluateOnNewDocument((entries) => {
  for (const [key, value] of Object.entries(entries)) {
    localStorage.setItem(key, value);
  }
}, consentStorage);

await page.goto('https://example.com', { waitUntil: 'networkidle2' });

This example only applies if inspection confirms that the target site uses localStorage and that setting the value before its scripts run is appropriate. A site may use another storage mechanism or validate consent server-side; adapt the capture and restoration process to its implementation.

Capture only after the restored state is in effect

  1. Accept consent on the target site once, or otherwise establish the desired consent state.
  2. Identify the state mechanism and record only the required data. For cookies, retain the relevant scope and attributes; for localStorage, identify the actual keys and values.
  3. On later runs, reuse the persistent profile or restore the saved state into the intended context before navigation.
  4. Navigate, verify that the banner is absent or reflects the expected choice, then call page.screenshot().

Puppeteer documents that BrowserContext.newPage(), Browser.newPage(), and Page.close() wait while a screenshot in the same context is in progress. That synchronization concerns page and context lifecycle during capture; it does not persist consent for a later run. See the Page.screenshot() reference.

Troubleshoot consent that does not persist

  • The banner returns after every run: Confirm that the launch uses the same userDataDir, or that the saved state is restored before navigation. Check whether your script deletes the profile or creates a fresh context.
  • Cookies were restored, but the banner remains: Verify that the site actually uses cookies for consent. Check cookie domain, path, expiration, and other relevant attributes; also look for localStorage or another mechanism.
  • It works in one context but not another: Context storage is isolated by design. Seed the new context with the required state rather than expecting it to inherit the previous context’s cookies or localStorage.
  • A cookie disappears or stops working: Check whether it was a session cookie and whether its scope and attributes were preserved. A session cookie’s lifetime semantics differ from those of a cookie with an expiration.
  • The profile launch fails or behaves inconsistently: Ensure the directory path is stable and writable, and that another process is not using the same profile concurrently.
  • Page-level cookie calls produce deprecation warnings: Move cookie reads and writes to the relevant Browser or BrowserContext methods.
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 you only need a clean website screenshot rather than a persistent Puppeteer session, ScreenshotNeo offers a screenshot API and MCP server. Its one-call API example is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for request options. Before capture, it accepts cookie/consent banners like a visitor and removes supported consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server provides screenshot and page information tools for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month without a card.

Frequently Asked Questions

Do cookies and localStorage carry between Puppeteer browser contexts?

No. Each context isolates its storage, so use the same persistent profile or explicitly restore the state into the new context.

Can I use a single consent cookie name for every website?

No. The target site determines the cookie name and may store consent in localStorage or another mechanism. Inspect the site before saving state.

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.