【问题标题】:Puppeteer refresh until selector value change? [duplicate]Puppeteer 刷新直到选择器值改变? [复制]
【发布时间】:2020-12-15 12:31:10
【问题描述】:

我希望让 puppeteer 刷新,直到我要注册的课程开放。我计划每 30 秒左右刷新一次页面,直到选择类的选择器打开。我已经走得足够远,可以登录并到达最后一页,但我无法绕过重复部分。正常的JS函数会报错。

page2.waitForNavigation()
await page2.waitFor(4000)
await page2.select('select[name="country"]', "USA")
await page2.click('#searchZip');

function findClass() {  
page2.waitForNavigation()
await page2.waitFor(4000)
await page2.click('#showAvailableOnly');
await page2.evaluate(() => {
let el = document.querySelector(".dataTables_empty")
result = el.innerText
if (result = "No matching records found") {
    console.log("Could not find any open test centers. Waiting 60 seconds to refresh.");
    

我想让 puppeteer 在这里等待 30 秒,然后再次运行 findClass() 之后的代码,直到 result 发生变化。

} else {
    console.log("Found opening!");
}})     
}

我会很感激任何帮助,因为我对 puppeteer 或一般的 nodejs 还是很陌生。非常感谢!

【问题讨论】:

标签: javascript node.js asynchronous puppeteer


【解决方案1】:

在 pupeteer 中有一个你可以使用的函数

await page.waitFor(30000);

或者您可以用作第二种解决方案

    function delay(time) {
   return new Promise(function(resolve) { 
       setTimeout(resolve, time)
   });
}

然后调用它

await delay(30000);

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2016-11-17
    • 2022-11-30
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多