50

I had to request the same webpage twice to get the cookies in the 1st request and use it in the 2nd request in the following example.

Could anybody show me the code to save the cookies in one puppeteer session and load it in another session so that there is no need to request the same webpage twice in the 2nd session? Thanks.

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');

    const linkHandlers = await page.$x("//div[@id='enhancers']//a[@data-track-event='Table See-All']");

    if (linkHandlers.length > 0) {
        const [response] = await Promise.all([
            page.waitForResponse(response => response.url().includes('/gene/api/data/Enhancers')),
            linkHandlers[0].click()
        ]);
        const resp_text = await response.text();
        console.log(resp_text);
    } else {
        throw new Error("Link not found");
    }
    await browser.close();
})();
1

3 Answers 3

78

To save the cookies, you can use the function page.cookies. To reuse the cookies, you can use the page.setCookies function.

Save cookies to disk

const fs = require('fs').promises;

// ... puppeteer code
const cookies = await page.cookies();
await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));

This will read the cookies for the current URL and save them to disk via JSON.stringify and fs.writeFile.

Reuse cookies

const fs = require('fs').promises;

// ... puppeteer code
const cookiesString = await fs.readFile('./cookies.json');
const cookies = JSON.parse(cookiesString);
await page.setCookie(...cookies);

To reuse the cookies, read the files from the disk via fs.readFile. Then parse the file content via JSON.parse. After that you have to call the page.setCookie function. As the function expects the cookies as arguments (and not as one array argument with cookies), we rely on the spread operator, which allows to call the setCookie function with the given cookies array as individual arguments.

9
  • Thanks but unfortunately this doesn't seem to work with the latest version of Puppeteer. The fs.writeFileSync results in: Error: TypeError: Cannot read property 'writeFileSync' of undefined Would you have any suggestion how to fix this?
    – RocketNuts
    Commented Sep 18, 2019 at 20:48
  • Actually I have created a minimal example.js that does just this. If I run that with node I'm getting that same error.
    – RocketNuts
    Commented Sep 18, 2019 at 20:49
  • @RocketNuts Thanks for the hint, I made a mistake there by mixing the old fs-API with the new promises API. The code is now updated. Commented Sep 19, 2019 at 5:39
  • 3
    This does not save nor restore HttpOnly cookies, so it is not a complete solution to keep session etc.
    – user239558
    Commented Aug 12, 2020 at 18:52
  • 2
    This example totally works, confirm. It saves httpOnly cookies. Thanks. Commented Jan 7, 2022 at 2:50
6

You can follow this code example below:

const puppeteer = require('puppeteer');
const fs = require('fs').promises; //for working with files

//save cookie function
const saveCookie = async (page) => {
    const cookies = await page.cookies();
    const cookieJson = JSON.stringify(cookies, null, 2);
    await fs.writeFile('cookies.json', cookieJson);
}

//load cookie function
const loadCookie = async (page) => {
    const cookieJson = await fs.readFile('cookies.json');
    const cookies = JSON.parse(cookieJson);
    await page.setCookie(...cookies);
}

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await loadCookie(page); //load cookie
  await page.goto('https://example.com');
  await saveCookie(page); //save cookie
  await browser.close();
})();

1

In 2024 with version 22.6.3 method described by Thomas using await page.cookies() and await page.setCookie(...cookies); is not working for me. Specifically setCookie is not setting them

I was able to fix it using CDP session.

Dumping:

const session = await page.target().createCDPSession();
const resp = await session.send('Network.getAllCookies');
await session.detach();
await fs.writeFile(`${SCRIPTDIR}/storage/cookies.json`, 
   JSON.stringify(resp.cookies, null, 2));

Restore:

const session = await page.target().createCDPSession();
const parsed = JSON.parse(cookiesString);
await session.send('Network.setCookies', {
  cookies: parsed,
});
await session.detach();
1
  • hello, I used your solution to login Google and save cookies but it doesn't work. Could you share with me the full code of this solution. Thanks in advance.
    – Tuan Pham
    Commented Apr 14 at 9:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.