2

I write a program with selenium in python. My goal is to find the src of the video in the page. This is my code

video_element = chrome_driver.find_element_by_tag_name("video")
video_src = video_element.get_attribute("src")

When I try to check video_src I get an empty string, however if I put time.sleep(1) before I try to acquire the src I get the real link to the video. I have tried to use WebDriverWait instead of time.wait like so

video_element = WebDriverWait(chrome_driver, 3).until(
            expected_conditions.element_to_be_clickable((By.TAG_NAME, "video"))
        )

But I couldn't find any condition that waits until the src tag is filled with the real link. Is there a way to wait with selenium instead of time? (with time it is not guarantee that the src will be filled)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Yedidya kfir
  • 1,419
  • 3
  • 17
  • 32
  • please provide your site or DOM – SeleniumUser Mar 25 '20 at 20:44
  • an example for a page in the site with a video is https://www.thewatchcartoononline.tv/www-working-episode-1-english-subbed. Notice that you have to switch to the frame of the video before you get the video element. Tell me if you require the full code – Yedidya kfir Mar 25 '20 at 20:52

3 Answers3

2

You can try with the below.

video_element = WebDriverWait(chrome_driver, 3).until(
            expected_conditions.presence_of_element_located((By.XPATH, "//video[not(@src='')]"))
        )
supputuri
  • 13,644
  • 2
  • 21
  • 39
2

Please try below solution before that you have to switch to iframe

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('https://www.thewatchcartoononline.tv/www-working-episode-1-english-subbed')
driver.switch_to.frame("anime-js-0")
video_element = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID, "video-js_html5_api")))
val = video_element.get_attribute("src")
print val

Output: enter image description here

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • I tried to do this before I switch and I got a timeout exception (because selenium could not found the video tag). when I tried your solution after the switch, it did work but only if I used timeout=20, if I used timeout=3 it didn't work. this tells me that it acted just like time.sleep – Yedidya kfir Mar 25 '20 at 21:01
  • I have encountered the same problem as before, please check your code with timeout=3 not 20, also you didn't use the chrome_option you created, did you mean to do that? – Yedidya kfir Mar 25 '20 at 21:15
  • I have removed chrome_option and changed WebDriverWait to 3 still its working fine for me. please check updated solution – SeleniumUser Mar 25 '20 at 21:19
  • You are correct, I forgot to change the locator to be with id and not tag name – Yedidya kfir Mar 25 '20 at 21:37
1

To extract the value of the src attribute you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.TAG_NAME, "video"))).get_attribute("innerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//video"))).get_attribute("src"))
    
  • 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