skip to Main Content

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


  1. You can stop connection when website trying to loads ads.
    Here example how to doing that :

    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.setRequestInterception(true);
    // Listen to the 'request' event
    page.on('request', (request) => {
    const url = request.url().toLowerCase();
    if (url.includes('ad') || url.includes('ads')) {
    // Block ads by aborting the request
       request.abort();
    } else {
      // Continue other requests
      request.continue();
    }
    });
    
    //Navigate to youtube
    await page.goto('https://youtube.com');
    
    Login or Signup to reply.
  2. here’s an updated code:

    const waitForAdSkip = async () => {
      while (true) {
        const skipButton = await videoPage.$('.ytp-ad-skip-button');
        if (skipButton) {
          const isHidden = await skipButton.evaluate((el) => el.offsetParent === null);
          const isDisabled = await skipButton.evaluate((el) => el.disabled);
          if (!isHidden && !isDisabled) {
            console.log('Ad is skippable. Skipping...');
            await skipButton.click();
            break;
          }
        }
        console.log('Ad is not skippable yet. Waiting...');
        await videoPage.waitForTimeout(2000);
      }
    };
    
    await waitForAdSkip();
    
    console.log('Ad skipped. Continuing with the rest of the script...');
    
    await videoPage.waitForTimeout(30000);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search