我正在使用puppeteer来运行无头镀铬。登录后,我想保留会话cookie以备将来使用。有没有办法在puppeteer中保存和加载会话cookie?
寻找类似的东西:
(async () => {
console.log('start -> ', true);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setUserAgent(agent);
await page.setViewport({width: 1280, height: 699});
await page.goto('https://www.website.com/');
await login(); // custom method for log in
// here --->
const sessionCookies = await page.cookies.toJSON();
// and in another session --->
await page.setCookies(cookiesJson);
})();
答案 0 :(得分:2)
您应该可以使用page.cookies()
// here --->
const sessionCookies = await page.cookies();
// and in another session --->
await page.setCookies(sessionCookies);