Home > database >  Selenium clicking video to start
Selenium clicking video to start

Time:02-25

Currently a beginner, I am trying to click a video so it would start playing:

<div ><video  tabindex="-1" disableremoteplayback="" webkit-playsinline="" playsinline="" x-webkit-airplay="deny" preload="metadata" src="/get_file/1/d9ce3f3e42f691a717900763bb60674bd97739d341/64000/64857/64857_hq.mp4/?d=3166&amp;br=626&amp;ti=1645665256" __idm_id__="147457" style="object-fit: fill;"></video></div>

I tried to use:

element = browser.find_element(By.CSS_SELECTOR, ".jw-media")
element.click()

as a css selector from right clicking the div from inspect elements and copying the css selector. I've also tried

element = browser.find_element(By.CLASS_NAME, "jw-video jw-reset")

but that doesn't work either, what am I doing wrong?

CodePudding user response:

The desired element is a dynamic element, to click() on the element 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, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.jw-media.jw-reset > video.jw-video.jw-reset[src]"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='jw-media jw-reset']/video[@class='jw-video jw-reset' and @src]"))).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
    

CodePudding user response:

So I found out that the answer to the question was because the site I was trying to access had an ad on top of the video player which opened a new tab once it clicked on the video but I couldn't tell because I use an adblocker. I installed an adblocker by downloading the .xpi file and installing it with selenium.

browser.install_addon(filelocation, temporary=True)

Thanks for the replies and help everyone.

  • Related