【问题标题】:How do you wait for a selector, but only for a specific time period in puppeteer?您如何等待选择器,但只等待 puppeteer 中的特定时间段?
【发布时间】:2019-08-14 15:02:02
【问题描述】:

我正在从 youtube 上抓取数据并尝试获取 cmets 的数量。我正在尝试获取包含该值的元素,但如果 cmets 对视频禁用,则该元素根本不存在,我认为 waitForSelector() 在结束程序之前等待大约 30 秒。我如何告诉 puppeteer 等待该元素,例如 5 秒,如果它不存在,则继续执行其余代码?

这是我正在使用的代码-

await page.waitForSelector("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")
let commentCount = await (await (await page.$("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")).getProperty("textContent")).jsonValue()

【问题讨论】:

标签: javascript node.js puppeteer


【解决方案1】:

下面的代码你可以试试等待-

await page.waitForSelector(yourSelector, {timeout: TIMEOUT});

例如:

await page.waitForSelector(yourSelector, {timeout: 5000});

更新:

抓住timeouterror并做点什么 -

const {TimeoutError} = require('puppeteer/Errors');

try {
  await page.waitForSelector(yourSelector, {timeout: 5000});
} catch (e) {
  if (e instanceof TimeoutError) {
    // Do something if this is a timeout.
  }
}

参考:

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectorselector-options

【讨论】:

  • 它工作了,但是当超时完成时它仍然停止执行。即使发生超时,如何继续执行剩余的代码?
  • 更新答案,请查收
  • 我是使用page.evaluate() 完成的,但感谢TimeoutError 上的信息。我不知道。我还将在此处发布代码作为可能需要它的任何人的答案。再次感谢!
【解决方案2】:

试试下面的代码,只需添加一个timeout 选项

try {
    await page.waitForSelector("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer", {timeout: 5000 }); // 5s timeout
} catch (e) {
   // something wrong !!!!!!
}

let commentCount = await (await (await page.$("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")).getProperty("textContent")).jsonValue()

【讨论】:

    【解决方案3】:

    我设法在浏览器中运行它并将其包含在page.evaluate() 中。如果将来有人需要,这是代码 -

    while(true){
        if(await page.evaluate(async () => { // scroll till there's no more room to scroll or the comment element shows up
            return await new Promise((resolve, reject) => {
               var scrolledHeight = 0  
               var distance = 100 
               var timer = setInterval(() => {
                   var scrollHeight = document.documentElement.scrollHeight
                   window.scrollBy(0, distance)
                   scrolledHeight += distance
                   if(scrolledHeight >= scrollHeight || document.querySelector("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")){
                         clearInterval(timer)
                         resolve(true)
                   }
               }, 500)
            })
        })){
            break
        }
    }
    let commentElement = await page.$("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer"), commentCount = null
    if(commentElement !== null){
        commentCount = await (await commentElement.getProperty('innerHTML')).jsonValue()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 2022-10-18
      • 2020-10-13
      相关资源
      最近更新 更多