I have an issue with cookie persistence in my Puppeteer automation setup
When I run my automation script and then restart the browser, all the cookies disappear. I expected them to stay saved since I’m using a custom user data directory.
Here’s my current setup:
const puppeteer = require('puppeteer');
async function startBrowser() {
const profilePath = './my-browser-data';
const launchOptions = {
headless: false,
userDataDir: profilePath,
defaultViewport: null,
ignoreHTTPSErrors: true,
args: [
'--no-sandbox',
'--disable-web-security',
'--start-maximized',
'--disable-features=VizDisplayCompositor'
]
};
const browserInstance = await puppeteer.launch(launchOptions);
const newPage = await browserInstance.newPage();
await newPage.goto('https://example.com');
return browserInstance;
}
startBrowser();
Any ideas why the cookies aren’t persisting? I thought setting userDataDir would handle this automatically.
The --disable-features=VizDisplayCompositor flag might be causing this. I had the exact same issue and removing that argument fixed cookie persistence for me. Also, wait a few seconds before closing the browser - Chrome needs time to write everything to disk.
Check if you’re handling browser closure properly in your process lifecycle. I hit this exact issue when my Node.js process died abruptly without letting Chrome write session data to disk. Fixed it by adding signal handlers to catch termination events. Try adding process.on('SIGINT', async () => { await browser.close(); process.exit(); }); for graceful shutdown. Also, some Chrome flags mess with data persistence - temporarily remove the --disable-web-security flag to see if it’s breaking your user data directory.
I encountered a similar issue previously. To resolve it, ensure that you’re closing the browser instance properly by using await browser.close() at the end of your script. Without this, Chrome might not save any session data to the specified user directory. Additionally, verify the permissions of the ./my-browser-data directory; sometimes, even if the folder is present, Chrome may lack the necessary permissions to write data. A good test is to create a file in that directory to confirm the permissions are set correctly.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.