thomasdondorf / puppeteer-cluster

Puppeteer Pool, run a cluster of instances in parallel
MIT License
3.24k stars 310 forks source link

waitForResponse does not work in cluster.tasks callback #448

Open kouroshezzati opened 2 years ago

kouroshezzati commented 2 years ago

I need to open 20 pages parallelly and click on a button then wait for a response after that get the data from a tag. and my code is:

async function getPageData(links) {
  return new Promise(async (resolve, reject) => {
    try {
      const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_PAGE,
        maxConcurrency: 200,
        monitor: true,
      });
      let allData = [];
      await cluster.task(async function getData({ page, data: url }) {
        await page.goto(url, {
          waitUntil: 'networkidle2',
        });
        const buttonQuery = 'button[role=tab]:first-child';
        const buttonElement = await page.waitForSelector(buttonQuery);
        await buttonElement.click(buttonElement);
        await page.waitForResponse('https://XXX'); // the problem is here
        const data = await page.evaluate(getList);
        const [oscillators, summary, movingAverage] = data;
        allData.push({ oscillators, summary, movingAverage });
      });
      links.map(async function addQueue(link) {
        cluster.queue(link);
      });

      await cluster.idle();
      await cluster.close();
      resolve(allData);
    } catch (e) {
      reject(e);
    }
  });

but it just work for the first time and ignore the rest of the tasks. but when I remove page.waitForResponse() the all tasks will be run as expected.

How can I make all tasks wait for their response then extract the data?