NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I save and load cookies in Puppeteer?

Read the cookies after the session is established, write them to a JSON file, and set them again at the start of the next run before you navigate to the site.

The short answer

Puppeteer can read every cookie the browser holds and set cookies before a page loads. Save them as JSON once you are logged in or past a check, and restore them at the start of the next run, before the first goto. Long-standing releases expose this as page.cookies() and page.setCookie(); newer releases also offer browser level browser.cookies() and browser.setCookie() and mark the page methods as deprecated. Check the docs for the version in your lockfile.

Saving and restoring with a small helper

import { readFile, writeFile } from "node:fs/promises";
import launcher from "puppeteer";

const JAR = "./session-cookies.json";

async function restoreJar(tab) {
  try {
    const saved = JSON.parse(await readFile(JAR, "utf8"));
    if (saved.length) await tab.setCookie(...saved);
  } catch { /* first run: nothing stored yet */ }
}

async function storeJar(tab) {
  const current = await tab.cookies();
  await writeFile(JAR, JSON.stringify(current, null, 2));
}

const session = await launcher.launch({ headless: true });
const tab = await session.newPage();
await restoreJar(tab);
await tab.goto("https://portal.example.com/dashboard");
// ...log in here if the dashboard redirected to a login form...
await storeJar(tab);
await session.close();

Restoring before navigation matters: the very first request then carries the session, so the site never sees an anonymous visit. Note that page.cookies() with no arguments returns cookies for the current page URL, so call it after you are on the site, or pass the URLs you care about.

What a saved cookie contains

Each entry carries name, value, domain, path, expires, httpOnly, secure and sameSite. Keep those fields. Dropping the domain or path makes the browser attach the cookie to the wrong requests or to none. Session cookies have expires set to -1 and would normally vanish when the browser closes; restoring them yourself keeps them alive across runs, until the server decides they have expired.

A persistent profile instead

If you want everything kept (cookies, local storage, IndexedDB, cached service workers), launch with a profile folder: puppeteer.launch({ userDataDir: "./profile-a" }). The browser writes its state there and loads it next time, with no code on your side. Many logins store tokens in local storage rather than cookies, so a cookie file alone may not keep you signed in. The trade-off is that a profile folder is large and should not be shared between parallel browsers.

Cookies from a cleared check

Some protection systems hand out a cookie once a check is passed: cf_clearance for a Cloudflare challenge, aws-waf-token for AWS WAF, datadome for DataDome. Saving these avoids a new check on every run, with two conditions. They are tied to the user agent, and often to the IP, that earned them, so reuse them with the same browser build and proxy. And they expire, so plan for a refresh. The page on reusing cf_clearance cookies covers the rules for the Cloudflare cookie.

If you obtain such a cookie through GateSolver, for example from an AWS WAF task or a Cloudflare Challenge task, set it with the same helper before navigating and keep the user agent from the solution.

Common mistakes

  • Saving before the session exists. Write the file after the redirect that follows login, not right after clicking submit.
  • Mixing accounts. One cookie file per account or identity. Loading two sessions into one browser confuses both.
  • Committing the file. A saved session works like a password. Keep it out of version control and out of shared logs.
  • Ignoring expiry. Check for a redirect to the login page after restoring, and fall back to a fresh login when it happens.

Moving cookies to a plain HTTP client

A common pattern is to log in once in the browser and then scrape with a fast HTTP client. Convert the saved list into a Cookie header (name=value pairs joined with ; ) or load it into your client's cookie jar, and send the same user agent the browser used. The broader question of managing cookies and sessions in automation covers rotation and expiry across many sessions.

Common questions

Do restored cookies keep me logged in forever?

No. The server decides how long a session is valid; restored cookies only last until it expires or is revoked.

Does page.cookies() return cookies for every domain?

By default it returns those for the current page URL. Pass more URLs, or use a browser level call in newer versions, to get others.

Should I save local storage as well?

If the site keeps its login token there, yes. A userDataDir profile saves it automatically.

Can I share one cookie file across parallel browsers?

For read-only browsing it often works, but the site may see one session from many places. Separate sessions are safer.

Why does a restored cf_clearance cookie not work?

It must be used with the same user agent and usually the same IP that obtained it, and it may have expired.

Keep reading