I’m trying to test to see if I can skip the ad on youtube using nodejs puppeteer.
This is just testing purposes, but it’s not working when it should be.
I put it in a while loop to find if it can be skipped, then click the class element. Though it’s not doing anything, and it just doesn’t work.
const waitForAdSkip = async () => {
while (true) {
const skipButton = await videoPage.$('.ytp-ad-skip-button');
if (skipButton) {
console.log('Ad is skippable. Skipping...');
// Check if the button is clickable
const isClickable = await skipButton.isIntersectingViewport();
if (isClickable) {
// Perform any desired action to skip the ad, like clicking the skip button.
await skipButton.click();
break;
}
}
console.log('Ad is not skippable yet. Waiting...');
// Wait for a few seconds before checking again.
await videoPage.waitForTimeout(2000);
}
};
// Wait for the "Skip Ad" button to appear and be clickable
await waitForAdSkip();
// Continue with the rest of your code after the ad is skipped
console.log('Ad skipped. Continuing with the rest of the script...');
// Wait for the rest of the code to execute
await videoPage.waitForTimeout(30000);
I was expecting it to skip the ad when the text of the element contains "Skip Ad" and then break the loop and click on skip ad.
2
Answers
You can stop connection when website trying to loads ads.
Here example how to doing that :
here’s an updated code: