Struggling to maintain cookies in Puppeteer after browser restart

Hey folks, I’m having a weird issue with Puppeteer. I’m trying to keep my cookies after closing and reopening the browser, but it’s not working as expected.

Here’s what I’m doing:

  1. Set cookies for an authenticated session
  2. Close the browser
  3. Reopen it later

The strange part is, if I log in during the session instead of manually setting cookies, it stays logged in when I reopen the browser. I’m thinking it might be related to userDataDir, but I’m not sure.

I’ve tried:

  • Using page.setCookie(...args)
  • Using network.send("setCookie", cookies)
  • Manually writing to userDataDir’s SQLite (no luck)

Here’s a simplified version of my code:

async function runTest() {
  const browser1 = await launchBrowser();
  const page1 = await browser1.newPage();
  
  await setCookies(page1);
  await page1.goto('https://example.com');
  
  await browser1.close();
  await wait(5000);
  
  const browser2 = await launchBrowser();
  const page2 = await browser2.newPage();
  await page2.goto('https://example.com');
}

function launchBrowser() {
  return puppeteer.launch({
    headless: false,
    userDataDir: './user_data',
    args: ['--no-first-run', '--no-default-browser-check']
  });
}

async function setCookies(page) {
  const cookiesData = loadCookiesFromFile();
  await page.setCookie(...cookiesData);
}

Any ideas on what I might be missing? Thanks in advance!

I’ve dealt with a similar issue before, and it can be frustrating. One thing that worked for me was ensuring the cookies were being set with the correct domain and path. Sometimes, if these aren’t specified correctly, the browser won’t recognize them on restart.

Also, have you tried using the puppeteer-extra plugin with puppeteer-extra-plugin-stealth? This combination can sometimes help with cookie persistence issues.

Another approach that might work is to use CDP (Chrome DevTools Protocol) directly to set cookies. It’s a bit more low-level, but it gives you more control:

await page.evaluate(async () => {
  await new Promise(resolve => {
    chrome.debugger.sendCommand({tabId: chrome.windows.WINDOW_ID_CURRENT}, 'Network.setCookies', {cookies: cookiesArray}, resolve);
  });
});

Lastly, double-check that your userDataDir is being properly persisted between sessions. Sometimes, file permissions or antivirus software can interfere with this.

Have u tried using a cookie manager library? smthing like ‘tough-cookie’ might help. Also, make sure ur cookies aren’t set to expire too soon. double-check the ‘expires’ and ‘maxAge’ fields. if all else fails, u could try serializing the entire session and restoring it instead of just the cookies.

I’ve encountered this issue before, and it can be quite tricky. One thing to consider is the cookie’s ‘sameSite’ attribute. If it’s set to ‘Strict’, it might not persist across sessions. Try setting it to ‘None’ or ‘Lax’ when you’re manually setting cookies.

Another potential solution is to use a custom storage mechanism. Instead of relying on browser cookies, you could store session data in a separate file or database, then load it when you relaunch the browser. This approach gives you more control over persistence.

Lastly, ensure you’re closing the browser properly. Sometimes, if the browser isn’t shut down cleanly, it can affect cookie persistence. You might want to add a try-catch block around your browser.close() call to handle any potential errors during shutdown.