Nodejs Puppeteer等待完成循环中的所有代码 [英] Nodejs Puppeteer Wait to finish all code from loop

查看:825
本文介绍了Nodejs Puppeteer等待完成循环中的所有代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用puppeteer抓取网页.在此网页中,我输入了一些文本,然后单击提交"按钮.单击后,页面将返回带有结果的表格.我想在匹配"somevar"时获得这些表结果. 问题是:for不能在循环之前完成所有功能.它已经进入下一个循环并再次填充输入,而不是先填充并首先获取结果,结果是:'test1test2'

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'

如何在循环之前让for for执行内部所有功能?

How to make the for execute all functions inside before looping?

callPup();

异步函数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*/
        }           
    }

})();   

}

推荐答案

您可以使用Promise串行同步运行循环,仅使用async之类的库会更容易.尝试使用eachSeries https://caolan.github.io/async/docs.html #eachSeries

You can run a loop synchronously in series using Promise, it would be easier to just use a library like async. Try using eachSeries https://caolan.github.io/async/docs.html#eachSeries

    function funcOk(){            
        async.eachSeries(textos, async (text) => {                     

            await page.type('#input1', text);

            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*/

            return Promise.resolve()
        })
    }

这篇关于Nodejs Puppeteer等待完成循环中的所有代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆