0

Im coding a selenium bot with Python which will be watching videos on different websites. I need it to press button to play video, but it doesnt work. I use Chrome webdriver and tried to use undetected_chromedriver, but nothing changed.

My webdriver:

browser = webdriver.Chrome(ChromeDriverManager().install())

or

browser = undetected_chromedriver.Chrome()

My code is:

sendContinue = browser.find_element(By.XPATH,'/html/body/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/a').click()

It worked on login page, but not here.

Button element:

<button class="ytp-large-play-button ytp-button ytp-large-play-button-red-bg" aria-label="Смотреть"><svg height="100%" version="1.1" viewBox="0 0 68 48" width="100%"><path class="ytp-large-play-button-bg" d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#f00"></path><path d="M 45,24 27,14 27,34" fill="#fff"></path></svg></button>

Can anyone solve this?

  • could you not just find the element by selecting it with the classname only? this worked for me. just do: `browser.find_elements(By.CLASS_NAME, "ytp-large-play-button").click()` should be the only element with the given class. – Dario Viva Jan 13 '23 at 18:33
  • Thank you, but it didnt work for me –  Jan 13 '23 at 18:52

2 Answers2

0

These kind of XPATH are unreliable, could you try the following?

sendContinue = browser.find_element(By.XPATH,"//*[@class='.ytp-large-play-button.ytp-button']").click()

Off topic, but it sometimes help to wait for the button to appear, like:

def wait_click(driver, xpath, delay = 20):
    try:
        myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, xpath)))
        myElem.click()
        return myElem
    except TimeoutException:
        print("Loading took too much time!")

You can call it with:

wait_click(driver, "//*[@class='.ytp-large-play-button.ytp-button.ytp-large-play-button-red-bg']")
Dominik
  • 26
  • 5
  • Thank you, but nothing worked for me, just printed "Loading took too much time!", so I got TimeoutException –  Jan 13 '23 at 18:54
  • so then you need to set the delay longer. you get an timeout exception because 20 is too low, try it with a huge number like 1000000. – Dario Viva Jan 13 '23 at 19:31
  • https://selenium-python.readthedocs.io/waits.html#explicit-waits when you read the documentation you would find that this delay in seconds, you probably tried it with 20 seconds, is the maximum amount of time that selenium tries to find the element, if your connection is so slow that the button is not there, even after 20 seconds you will get an TimeoutException, nothing weird happening here. – Dario Viva Jan 13 '23 at 19:34
  • Can you go in your browser console (Ctrl + Shift + c) then confirm this plays it? $('.ytp-play-button').click() Also I saw mistakes in my answer and I corrected it, you may retry – Dominik Jan 13 '23 at 19:54
  • when i plays it, i got r.fn.init [prevObject: r.fn.init(1)] –  Jan 14 '23 at 08:18
  • and your edited code doesnt work –  Jan 14 '23 at 08:19
0

To interact with any clickable element 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(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ytp-large-play-button.ytp-button.ytp-large-play-button-red-bg[aria-label='Смотреть'] > svg > path"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 20).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='Смотреть']"))).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