How can I handle multiple pages simultaneously in Puppeteer?
Gable E
gable e profile pic

Handling multiple pages simultaneously in Puppeteer involves creating multiple page instances and performing actions on each page independently. Here's a detailed explanation of how to handle multiple pages in Puppeteer: 1. Launching a new browser instance and creating multiple page instances:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

   const puppeteer = require('puppeteer');

   (async () => {
     const browser = await puppeteer.launch();
     const page1 = await browser.newPage();
     const page2 = await browser.newPage();
     // Create additional page instances as needed

     // Perform actions with the pages here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates multiple page instances using thenewPage() method. You can create as many page instances as required. 2. Navigating and interacting with multiple pages: Once you have multiple page instances, you can navigate and interact with each page independently.

1
2
3
4
5
6
7
8
9
10
11
12
13

   const page1 = await browser.newPage();
   const page2 = await browser.newPage();

   await page1.goto('https://example.com');
   await page2.goto('https://google.com');

   // Interact with page1
   // Perform actions, extract data, etc.

   // Interact with page2
   // Perform actions, extract data, etc.
   

In this example, page1 and page2 are two different page instances. You can use thegoto() method to navigate each page to a different URL. After navigating, you can perform various actions, such as clicking elements, filling forms, extracting data, or interacting with the page in any way you need. 3. Managing page events and lifecycle: Each page instance has its own events and lifecycle. You can register event listeners on individual page instances and handle them accordingly.

1
2
3
4
5
6
7
8
9

   page1.on('load', () => {
     console.log('Page 1 loaded');
   });

   page2.on('load', () => {
     console.log('Page 2 loaded');
   });
   

In this example,'load' events are registered on page1 and page2 to log a message when each page finishes loading. You can listen to other events such as'request','response', or'dialog', and handle them independently for each page. 4. Closing individual pages: After you have finished using a page, it's important to close it to free up resources and manage the lifecycle of each page.

1
2
3
4

   await page1.close();
   // Close additional pages as needed
   

Use theclose() method on the page instance to close it. Closing a page does not close the entire browser instance, but only the specific page. By following these steps, you can handle multiple pages simultaneously in Puppeteer. By creating multiple page instances, you can navigate, interact, and perform actions on each page independently. This enables you to automate tasks across multiple pages and manage the lifecycle of each page individually.