I’m working on a Puppeteer script where I need the browser to retain cookies across sessions. However, I’ve noticed that after closing the browser and reopening it, the cookies are lost.
Here’s a reproducible example of my code:
import puppeteer from 'puppeteer';
(async () => {
const userDataDir = 'C:\junk_dir\zxc';
const args = [
...puppeteer.defaultArgs({ headless: false }).map(arg => {
if (arg.startsWith('--disable-features')) {
return arg.replace('Translate,', '').replace(',Translate', '');
}
return arg;
}),
'--start-maximized',
'--disable-sync',
'--no-sandbox',
'--ignore-certificate-errors'
].filter(arg => !['--enable-automation', '--disable-extensions'].includes(arg));
const browser = await puppeteer.launch({
args,
acceptInsecureCerts: true,
headless: false,
ignoreHTTPSErrors: true,
defaultViewport: null,
ignoreDefaultArgs: true,
userDataDir: userDataDir,
pipe: true
});
// Open a new page
const page = await browser.newPage();
//
})();
Could you please advise?
Yaniv Levi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.