Search code examples
javascriptnode.jspuppeteerchromiumplaywright

Is there a way to open multiple tabs simultaneously on Playwright or Puppeteer to complete the same tasks?


I just started coding, and I was wondering if there was a way to open multiple tabs concurrently with one another. Currently, my code goes something like this:

const puppeteer = require("puppeteer");

const rand_url = "https://www.google.com";

async function initBrowser() {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto(rand_url);
  await page.setViewport({
    width: 1200,
    height: 800,
  });

  return page;
}

async function login(page) {
  await page.goto("https://www.google.com");
  await page.waitFor(100);
  await page.type("input[id ='user_login'", "xxx");
  await page.waitFor(100);
  await page.type("input[id ='user_password'", "xxx");
}

this is not my exact code, replaced with different aliases, but you get the idea. I was wondering if there was anyone out there that knows the code that allows this same exact browser to be opened on multiple instances, replacing the respective login info only. Of course, it would be great to prevent my IP from getting banned too, so if there was a way to apply proxies to each respective "browser"/ instance, that would be perfect.

Lastly, I would like to know whether or not playwright or puppeteer is superior in the way they can handle these multiple instances. I don't even know if this is a possibility, but please enlighten me. I want to learn more.


Solution

  • You can use multiple browser window as different login/cookies.
    For simplicity, you can use the puppeteer-cluster module by Thomas Dondorf.
    This module can make your puppeteer launched and queued one by one so that you can use this to automating your login, and even save login cookies for the next launches.
    Feel free to go to the Github: https://github.com/thomasdondorf/puppeteer-cluster

    const { Cluster } = require('puppeteer-cluster')
    
    (async () => {
      const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_CONTEXT,
        maxConcurrency: 2, // <= this is the number of
                           // parallel task running simultaneously
      })                   // You can change to the number of CPU
      const cpuNumber = require('os').cpus().length // for example
    
      await cluster.task(async ({ page, data: [username, password] }) => {
        await page.goto('https://www.example.com')
        await page.waitForTimeout(100)
        await page.type('input[id ="user_login"', username)
        await page.waitForTimeout(100)
        await page.type('input[id ="user_password"', password)
        const screen = await page.screenshot()
        // Store screenshot, Save Cookies, do something else
      });
    
         cluster.queue(['myFirstUsername', 'PassW0Rd1'])
         cluster.queue(['anotherUsername', 'Secr3tAgent!'])
      // cluster.queue([username, password])
      // username and password array passed into cluster task function 
      // many more pages/account
    
      await cluster.idle()
      await cluster.close()
    })()
    

    For Playwright, sadly still unsupported by the module above,
    you can use browser pool (cluster) module to automating the Playwright launcher.

    And for proxy usage, I recommend Puppeteer library as the legendary one.

    Don't forget to choose my answer as the right one, if this helps you.