puppeter loop dont iterate correctly

  iteration, javascript, loops

I don’t understand why puppetter dont iterate correctly the file maybe because its async the list of the file. its ok until 10 and after it the file is 14,18,22,24,
I’ve got an error error: TypeError: Cannot read property ‘Symbol(Symbol.iterator)’ of undefined. There is sommething wrong wiht the loop :

const puppeteer = require('puppeteer');
const fs = require("fs");
process.setMaxListeners(Infinity); 
const idMatchs = require('codematch.js');
// console.log(idMatchs);
var tabScore =[];
for (var j=0; j<idMatchs.length; j++){
    tabScore.push(idMatchs[j].substring(4,12))
}

var urls = [];
for (let j = 0; j<tabScore.length; j++) {
        urls.push("https://www.flashresultats.fr/match/" + tabScore[j] + "/#resume-du-match");
};

function load(urls){
  var f=0;
  for (let i = 0; i < tabScore.length; i++) {
  puppeteer.launch().then(async browser => {

  const page = await browser.newPage();

  await page.goto(urls[i], { waitUntil: 'networkidle2',timeout:0 });

  await page.click('#a-match-head-2-head');
  await page.waitFor(5000);


  page.waitForSelector('#h2h-home > span > a');//voir la valeur
  await page.click('#h2h-home > span > a');
  await page.waitFor(6000);
  await page.click('#tab-h2h-home > div:nth-child(1) > table > tbody > tr.hid > td > a');
  const html = await page.content();
  fs.writeFileSync("files/home" + String(i) + ".html", html);
  await page.waitFor(5000);
  await page.waitFor(5000);
  await page.click('#h2h-away > span > a');
  await page.waitFor(6500);
  await page.click('#tab-h2h-away > div:nth-child(1) > table > tbody > tr.hid > td > a');

  fs.writeFileSync("files/away" + String(i) + ".html", html);
  await page.waitFor(5000);
  await Promise.all();
  await browser.close();
    f++;

}).catch(e => console.log(`error: ${e}`));
  }
}

load(urls);

Source: Ask Javascript Questions

LEAVE A COMMENT