如何等待使用Selenium和Python定义src属性

问题描述 投票:1回答:3

我在python中用硒编写了一个程序。我的目标是在页面中找到视频的src。这是我的代码

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

[当我尝试检查video_src时,我得到一个空字符串,但是,如果我在尝试获取src之前放了time.sleep(1),则会获得到视频的真实链接。我已尝试使用WebDriverWait而不是time.wait像这样

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

但是我找不到任何条件,直到src标记填充了真实链接。有没有办法等待硒而不是时间? (随着时间的推移,不能保证src将被填充)

python-3.x selenium xpath webdriverwait tagname
3个回答
0
投票

您可以尝试以下。

video_element = WebDriverWait(chrome_driver, 3).until(
            expected_conditions.presence_of_element_located((By.XPATH, "//video[not(@src='')]"))
        )

0
投票

要提取src属性的值,您必须为所需的visibility_of_element_located()引入WebDriverWait,并且可以使用以下Locator Strategies中的任何一个:

  • 使用CSS_SELECTOR

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

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//video"))).get_attribute("src"))
    
  • Note:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

0
投票

[请先尝试以下解决方案,然后再切换到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.common.exceptions import TimeoutException
from selenium import webdriver
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
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, 20).until(EC.visibility_of_element_located((By.ID, "video-js_html5_api")))
print video_element.text
val = video_element.get_attribute("src")
print val 
© www.soinside.com 2019 - 2024. All rights reserved.