如何在Headless Chrome中将会话cookie从一个选项卡传递到另一个选项卡

时间:2017-09-07 15:25:56

标签: javascript google-chrome google-chrome-headless puppeteer

我正在使用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);
})();

1 个答案:

答案 0 :(得分:2)

您应该可以使用page.cookies()

// here --->
const sessionCookies = await page.cookies();

// and in another session --->
await page.setCookies(sessionCookies);