Selenium 找不到 iframe 元素

Selenium is not finding the iframe element

这是我要抓取的网站https://anime-hayai.com/play/30148/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-1-hd.html

我想从视频中抓取 scr='....' 数据,但它返回空字符串。

我试过的

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://anime-hayai.com/play/30148/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-1-hd.html')

video = driver.find_element_by_xpath("//*[@id='player']/div[2]/div[4]/video").text
print(video)

返回 '' 空字符串。

我做错了什么吗? enter image description here 视频 scr 的预期结果

'https://stream.anime-hayai.com/videoplayback?id=6o7ov8-mxpu0aKZQYtLDpMuepaDas4tllm2jqqGUcqDE1c-j0pzahY1pxGuiaJhSteDG5NLdkaaVcotklmRye55ecaGWoJqio46hcs2XyKSmaKZQYqCXoovq'

https://anime-hayai.com/player/30148 不是文本属性值而不是 src 属性值。
你的定位器也不太好。 所以试试这个

iframe_src = driver.find_element_by_xpath("//iframe[@name='video_player']").get_attribute("src")

您可能必须在该代码行之前添加延迟/等待,以便在提取此元素内容之前让页面完全加载。
此外,您可能必须切换到此 iframe 才能访问它。因此,如果上述解决方案仍然无效,请尝试以下操作:

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

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://anime-hayai.com/play/30148/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-1-hd.html')

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='video_player']"))) 

iframe_src = driver.find_element_by_xpath("//iframe[@name='video_player']").get_attribute("src")
print(iframe_src)

有嵌套的iframe,所以首先你要

  1. 切换到第一个 iframe
  2. 切换到子 iframe

此外,您需要一直向下滚动。

代码:

chromedriver_autoinstaller.install()
driver_path = r'C:\Users\panabh02\OneDrive - CSG Systems Inc\Desktop\Automation\chromedriver.exe'

driver = webdriver.Chrome(driver_path)
driver.maximize_window()

wait = WebDriverWait(driver, 30)


driver.get("https://anime-hayai.com/play/30148/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-1-hd.html")
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='video_player']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[class='embed-responsive-item']")))
video_url = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='jw-media jw-reset']//*"))).get_attribute('src')
print(video_url)

进口:

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

输出:

https://stream.anime-hayai.com/videoplayback?id=6o7ov8-mxpu0aKZQYtLDpMuepaDas4tllm2jqqGUcqDE1c-j0pzahY1pxGuiaJhSteDG5NLdkaaVcotklmRye55ecaGWoJqio46hcs2XyKSmaKZQYqCXoovq