Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98792

Issue with python WebDriverWait on youtube page

$
0
0

I'm trying to create a code that automatically presses the youtube video button "Skip Ad" if it is on the page, only if the page is playing a youtube video.

To do this I used the selenium webdriver module, calling the WebDriverWait function and waiting for the button presence, but even if the button appears on the page, the program doesn't recognize it and keeps endlessly waiting.

Image 1:

image 1

Image 2:

image 2

My code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('https://youtube.com')

while True:
    url = driver.current_url
    time.sleep(1)
    if 'https://www.youtube.com/watch' in url:
        button = WebDriverWait(driver, 10000).until(EC.presence_of_element_located((By.CLASS_NAME, 'ytp-ad-skip-button ytp-button')))
        button.click()

If I use driver.find_element_by_class_name('ytp-ad-skip-button ytp-button') it returns selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:{"method":"css selector","selector":".ytp-ad-skip-button ytp-button"}, so it definitely doesn't recognize the element. Why?

I've also tried waiting for presence of the element by xpath and by id, but it still doesn't work in the same way.

Looking for solutions in the web I found out that I had to switch to an iframe window using iframe = driver.find_element_by_tag(frame) and then driver.switch_to.frame(iframe). I also tried this, but it doesn't work.

What's wrong?


Viewing all articles
Browse latest Browse all 98792

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>