How to save and load cookies in Puppeteer?

Saving and loading cookies with Puppeteer is very straightforward, we can use the page.cookies() method to get all the cookies of a webpage, and use the page.setCookie() method to load cookies into a web page:

const puppeteer = require('puppeteer');

(async () => {

  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Open ScrapingBee's URL
  await page.goto('http://scrapingbee.com');

  // Get all the page's cookies and save them to the cookies variable
  const cookies = await page.cookies();

  // Open a second website
  await page.goto('http://httpbin.org/cookies');

  // Load the previously saved cookies
  await page.setCookie(...cookies);

  // Get the second page's cookies
  const cookiesSet = await page.cookies();

  console.log(JSON.stringify(cookiesSet));

  await browser.close();

})();

Related Puppeteer web scraping questions: