I'm using puppeteer to scrap a webpage. In this webpage, i input some text and click the submit button. Once clicked, the page will return a table with results. I want to get these table results when matches 'somevar'.
The problem is: The for is not completing all functions before looping. Instead of filling the input and get the results first, it already goes to the next loop and fill the input again, resulting in something like: 'test1test2'
How to make the for execute all functions inside before looping?
callPup();
async function callPup(){
'use strict';
const puppeteer = require('puppeteer');
const textos = ['test1','test2'];
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('http://localhost/teste.html');
await page.waitForSelector('#input1').then(funcOk());
async function funcOk(){
for (let i = 0; i < textos.length; i++) {
await page.type('#input1', textos[i]);
await page.keyboard.press('Enter');
/*get table results*/
const data = page.evaluate(() => {
const tds = Array.from(document.querySelectorAll('table tr td a'))
return tds.map(a => {
var txt = a.innerHTML;
return txt.replace(/<a [^>]+>[^<]*</a>/g, '').trim();
});
});
/*get table results*/
/*get only valid results*/
let j = 0;
for (let z = 0; z < data.length; z++) {
if(data[z] == someVar[i].num.toString()){
j = j + 1;
}
if(j <= 14){
console.log(data[z]);
j = j + 1;
}
}
/*get only valid results*/
}
}
})();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…