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 is what I use:
def youCheckAdRunning(self):
""" NOTE: When ad starts playing it opens a new PulseAudio instance.
1) Turn down volume immediately
2) Test what happens when job is killed?
NOTE: Player status will be "-1" while ad is running or "3" if ad
is starting. May also be "5" (in between songs in a playlist)
:return: True if ad displaying, False if no ad displayed
"""
try:
# TODO Start Duration Countdown
_ad = self.webDriver.find_element(
By.CSS_SELECTOR, ".ytp-ad-duration-remaining")
return True
except (NoSuchElementException, WebDriverException):
return False
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