How to manage multiple tabs in Puppeteer?

In Puppeteer, you can manage multiple tabs by using the browser object to create new tabs and navigate between them. Here is an example of how you can open multiple tabs and switch between them:

  1. Create a new Puppeteer browser instance:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); })();
  1. Open a new tab within the browser and navigate to a URL:
const page = await browser.newPage(); await page.goto('https://www.example.com');
  1. Open another tab and navigate to a different URL:
const page2 = await browser.newPage(); await page2.goto('https://www.anotherexample.com');
  1. Switch between tabs by using the page.bringToFront() method:
await page.bringToFront(); // Switch to the first tab await page2.bringToFront(); // Switch to the second tab

You can also close tabs using page.close() method:

await page.close(); // Close the first tab

By following these steps, you can effectively manage multiple tabs in Puppeteer.