How to Save and Load Cookies in Puppeteer?

Saving and loading cookies with Puppeteer is a powerful way to maintain session state across multiple pages. This can be particularly useful for web scraping tasks where you need to navigate between different pages without losing your session data. You can use the page.cookies() method to retrieve all cookies from a webpage and the page.setCookie() method to load cookies into a webpage. For a detailed understanding of what HTTP cookies are, check out this blog post.

Here’s a more comprehensive example of how to save and load cookies with Puppeteer:

      const puppeteer = require('puppeteer');

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

  // Open the first website and wait for it to load
  await page.goto('https://example.com', { waitUntil: 'networkidle2' });

  // Save all cookies from the first page
  const cookies = await page.cookies();
  console.log('Cookies from example.com:', cookies);

  // Open a second website and wait for it to load
  await page.goto('https://httpbin.org/cookies', { waitUntil: 'networkidle2' });

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

  // Get and log the cookies from the second page to verify they were set
  const cookiesSet = await page.cookies();
  console.log('Cookies set in httpbin.org:', cookiesSet);

  await browser.close();
})();

    

Explanation of the Code

  1. Launch Puppeteer and Open a New Page: const browser = await puppeteer.launch(); const page = await browser.newPage();
  2. Navigate to the First Website and Wait for it to Load Completely: await page.goto('https://example.com', { waitUntil: 'networkidle2' });
    The waitUntil: 'networkidle2' option ensures that the page has fully loaded before proceeding.
  3. Retrieve and Log Cookies from the First Website: const cookies = await page.cookies(); console.log('Cookies from example.com:', cookies);
  4. Navigate to the Second Website and Wait for it to Load Completely: await page.goto('https://httpbin.org/cookies', { waitUntil: 'networkidle2' });
  5. Load the Saved Cookies into the Second Website: await page.setCookie(...cookies);
  6. Retrieve and Log the Cookies from the Second Website to Verify: const cookiesSet = await page.cookies(); console.log('Cookies set in httpbin.org:', cookiesSet);
  7. Close the Browser: await browser.close();

By following these steps, you can ensure that cookies are saved and loaded correctly between different web pages using Puppeteer. This method allows you to maintain session states effectively during web scraping tasks.

For more advanced web scraping solutions, consider using Bright Data’s Puppeteer Browser. It offers an all-in-one solution to handle automated unblocking infrastructure, bypassing CAPTCHAs, and scaling your scraping projects effortlessly.

Ready to get started?