gpt4 book ai didi

python - 无法使用python selenium获取动态元素

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:23 25 4
gpt4 key购买 nike

site ,流式传输 youtube 视频。我想和他们一起获得播放列表。因此,我使用 selenium webdriver 获取所需的元素 div,其类名 ytp-title-text 是 youtube 链接所在的位置。

例如,当我使用浏览器控制台查找元素时,它位于此处:

<div class="ytp-title-text"><a class="ytp-title-link yt-uix-sessionlink" target="_blank" data-sessionlink="feature=player-title" href="https://www.youtube.com/watch?v=VyCY62ElJ3g">Fears - Jono McCleery</a><div class="ytp-title-subtext"><a class="ytp-title-channel-name" target="_blank" href=""></a></div></div>

我写了简单的测试脚本:

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

driver = webdriver.Firefox()
driver.get('http://awsmtv.com')

try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "ytp-title-text"))
)
finally:
driver.quit()

但是没有找到元素,抛出超时异常。我不明白,selenium 需要执行什么操作才能获得完整的页面源。

最佳答案

所需的链接是隐藏的,也位于 iframe 中。尝试在下面找到它:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("tvPlayer_1"))
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "ytp-title-link")))
print(element.get_attribute('href'))
finally:
driver.quit()

关于python - 无法使用python selenium获取动态元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52668208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com