-5

I'm writing a selenium code, but when I want to play a video clicking on the Play button, I realized that I can't play it. I used XPATH, CLASS_NAME, CSS_SELECTOR but it failed.

I used this line of code:

start_video = driver.find_element(By.XPATH,"//button[@type='button' and @aria-label='Play' ]").click()

shows Nosuchelementexception.

HTML of the element:

<button class="ytp-large-play-button ytp-button ytp-large-play-button-red-bg" aria-label="Play"></button>

Any help?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dax
  • 1
  • 2
    Welcome to Stack Overflow. You need to include more details in your code and your explanation. – ewokx Jun 19 '23 at 23:37
  • Firstly, thank you. I couldn't find any other code structure but there is this one, I used this one: start_video = driver.find_element(By.XPATH,"//button[@type='button' and @aria-label='Play' ]"). click() i got nosuchelementexception error – dax Jun 19 '23 at 23:42
  • your element might not be loaded. your element might be in an iframe. your element might be obstructed by a pop up or not visible. Try to use ```expected conditions``` – Rolandas Ulevicius Jun 20 '23 at 05:48

1 Answers1

-1

Given the HTML:

<button class="ytp-large-play-button ytp-button ytp-large-play-button-red-bg" aria-label="Play"></button>

To click on the Play button ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ytp-large-play-button.ytp-button.ytp-large-play-button-red-bg[aria-label='Play']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ytp-large-play-button ytp-button ytp-large-play-button-red-bg' and @aria-label='Play']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • First of all, thank you for your answer. I tried both of the codes you gave, but the result was disappointing again. This is Exactly My Error Code: – dax Jun 20 '23 at 07:37
  • WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ytp-large-play-) button.ytp-button.ytp-large-play-button-red-bg[aria-label='Play']"))).click() File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/support/wait.py", line 95, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: stacktrace: RemoteError WebDriverError NoSuchElementError element.find/ – dax Jun 20 '23 at 07:38
  • @dax Which website are you looking at? Is it a public url? Youtube? Share the link please. – undetected Selenium Jun 20 '23 at 21:03