admin管理员组文章数量:1026989
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?
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?
Share Improve this question edited Dec 24, 2024 at 11:30 Yaniv Levi asked Dec 24, 2024 at 10:45 Yaniv LeviYaniv Levi 92 bronze badges1 Answer
Reset to default 0If you use the userDataDir option it will save some cookies but not session cookies.
You can save those with page.cookies()
and set them later with page.setCookie()
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?
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?
Share Improve this question edited Dec 24, 2024 at 11:30 Yaniv Levi asked Dec 24, 2024 at 10:45 Yaniv LeviYaniv Levi 92 bronze badges1 Answer
Reset to default 0If you use the userDataDir option it will save some cookies but not session cookies.
You can save those with page.cookies()
and set them later with page.setCookie()
本文标签: Puppeteerwhy are Chrome cookies lost after closing the browser and reopeningStack Overflow
版权声明:本文标题:Puppeteer, why are Chrome cookies lost after closing the browser and reopening? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1735958689a1367440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论