How To Passing Multiple Data in Puppeteer-Cluster

1.7k Views Asked by At

Just one question. How can i do this?
I have these data :
url : http://example.com
and 2 string data, example : firstName and lastName

The url is still the same in every browser, but, firstName and lastName will be changed every browser (firstName1 lastName1 for browser1, firstName2 lastName2 for browser2 and so on)

How I can pass these data from cluster.queue to cluster.task?

1

There are 1 best solutions below

0
On BEST ANSWER

If you have more than one data then you can pass it as an array or object.

const { Cluster } = require('puppeteer-cluster');

(async () => {
  const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_CONTEXT,
    maxConcurrency: 2,
  });

  await cluster.task(async ({ page, data: {username, password} }) => {
    await page.goto(url);
    const screen = await page.screenshot();
    // Store screenshot, do something else
  });

  cluster.queue({username: 'john.doe', password: '53CR37A63N7!'});
  cluster.queue({username: 'mr.bean', password: 'JohnyEnglish1234'});
  // many more pages

  await cluster.idle();
  await cluster.close();
})();