3

I’ve been trying to use python selenium to watch YouTube videos for me and collect data.

Getting the data is fairly easy, however, I run into problems when an ad pops up in YouTube.

For some reason I can't figure out how to detect whether or not I have an ad

My current function is:

def check_ad():
 try:
            WebDriverWait(driver, 20).until(         EC.presence_of_element_located(driver.find_element_by_xpath('//*[@id="simple-ad-badge:g"]'))
            )
          print("Ad detected")
 except:
            print("No Ad")

Does anyone know any other way I can do this?

I've tried multiple different element selections, including the advertisment website link, the presence of the skip ad button, the little ad blurb at the bottom but nothing seems to work. I've also tried look for class name and id.

  • This doesn't exactly answer your question, so I'm writing it here as a comment- maybe [dynamically inject an adblocking extension](https://stackoverflow.com/a/52420595/11985743) into the Webdriver? – Xiddoc Dec 27 '22 at 08:48

0 Answers0