I'm scraping data from youtube and trying to get the number of comments. I'm trying to grab the element that contains the value, but in case the comments are disabled for a video, that element doesn't exist at all and waitForSelector()
waits, I think, for about 30 seconds before ending the program. How can I tell puppeteer to wait for that element for, say, 5 seconds and if it doesn't exist, move on with the rest of the code?
Here's the code that I'm using-
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()
Below code you can try for waiting -
await page.waitForSelector(yourSelector, {timeout: TIMEOUT});
Ex:
await page.waitForSelector(yourSelector, {timeout: 5000});
UPDATED:
To catch timeouterror
and do something -
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.
}
}
Reference:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectorselector-options
Try code below, just add a timeout
option
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With