javascript 如何在puppeteer中等待一个选择器,但只在特定的时间段内?

dohp0rv5  于 2024-01-05  发布在  Java
关注(0)|答案(3)|浏览(145)

我正在从youtube上抓取数据,并试图获取评论的数量。我试图获取包含该值的元素,但如果视频的评论被禁用,该元素根本不存在,我想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()

字符串

0lvr5msh

0lvr5msh1#

下面的代码你可以尝试等待-
第一个月

  • 例如:*

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://pptr.dev/api/puppeteer.page.waitforselector
旧版:https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectorselector-options

qgzx9mmu

qgzx9mmu2#

尝试下面的代码,只需添加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()

字符串

sg24os4d

sg24os4d3#

我设法在浏览器中运行它,并将其包含在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()
}

字符串

相关问题