【问题标题】:Puppeteer browser and page instant close, page.evaluate not working on bloc 2Puppeteer 浏览器和页面即时关闭,page.evaluate 不适用于 bloc 2
【发布时间】:2020-04-29 13:18:46
【问题描述】:

我有这个脚本,在第 2 块开始之前一切正常 我不明白为什么它在第 2 块中不起作用,我应该在“page.on request”中有一个返回,但它不是直接离开的情况,你知道这个问题吗?

节点不返回错误给我

谢谢

async function main() {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.setViewport({width: 1200, height: 720})
    await page.goto('https://site.local', { waitUntil: 'networkidle0' }); // wait until page load
    await page.type("input[name='UserName']", "myusername");
    await page.type("input[name='Password']", "mypassworduser");
    // click and wait for navigation
    await Promise.all([
            page.click("body > main > button"),
            page.waitForNavigation({ waitUntil: 'networkidle0' }),
    ]);

    await page.goto(urlformation);

    await page.setRequestInterception(true);

    await page.on('request', (request) => { 
      if (request.resourceType() === 'media') {
        var CurrentRequest  = request.url();
        console.log(CurrentRequest);
        fs.appendFileSync(fichiernurlaudio, request.url()+"\r\n"); 
      }
      request.continue();

    }); 
//START BLOC 1 ------------------IT WORK    
    const Titresaudios = await page.evaluate(() => {
        let names = document.querySelectorAll(
            "td.cursor.audio"
        );
        let arr = Array.prototype.slice.call(names);
        let text_arr = [];
        for (let i = 0; i < arr.length; i += 1) {
            text_arr.push($vartraited+"\r\n");
        }
        return text_arr;
    })
    fs.appendFileSync(fichiernomaudio, Titresaudios);
//END BLOCK 1------------------IT WORK- i got data in my file   

//START BLOCK 2-------seems to ignore-----------NOT WORKING
    await page.evaluate(()=>{

        let elements = document.querySelectorAll("td.cursor.audio");    

        elements.forEach((element, index) => {
                setTimeout(() => {
                    element.click();
                }, index * 1000);  
        })

    })
//END BLOCK 2---------seems to ignore---------NO WORKING 

//i should see some console.log in page.on('request' (request) => { but instant close after works of bloc 1


    await page.close();
    await browser.close();


}

main();

【问题讨论】:

    标签: javascript browser puppeteer evaluate


    【解决方案1】:

    我不知道你到底想在那里实现什么,但该块可以这样重写:

    // ...
    const els = await page.$$( 'td.cursor.audio' );
    for( const el of els ) {
    
      // basically your timeout, but from outside the browser
      await page.waitFor( 1000 );
    
      // perform the action
      await el.click();
    
    }
    // ...
    

    在您的脚本中,您在evaluate() 调用中所做的唯一一件事就是安排一些超时操作。一旦这些被安排(但未执行!)evaluate() 的回调退出并且您的脚本继续关闭浏览器。很可能您的点击从未被执行。


    根据我的经验,通常建议在 NodeJS 中而不是在浏览器中做尽可能多的事情。通常也便于调试。

    【讨论】:

    • 我不确定,为什么要在这里使用硬编码延迟。如果您的操作有某种结果(例如,在某处添加了一个新类),请等待。它比任何原因都可以超过的固定时间稳定得多。
    猜你喜欢
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多