Search code examples
python-3.xseleniumxpathwebdriverwaittagname

How to wait until the src attribute is defined using Selenium and Python


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)


Solution

  • 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