【问题标题】:Selenium is not finding the iframe elementSelenium 找不到 iframe 元素
【发布时间】:2021-12-30 02:15:05
【问题描述】:

这是我要抓取的网站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'

【问题讨论】:

    标签: jquery python-3.x selenium web-scraping iframe


    【解决方案1】:

    有嵌套的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
    

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

      • 我得到了同样的错误,就像你的代码给NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[@name='video_player']"}
      • 你是否尝试过两种方式:直接访问iframe元素并尝试先切换到它然后访问它?
      • 更新的问题...我也尝试了这两种...你能检查你的系统吗?
      猜你喜欢
      • 1970-01-01
      • 2016-05-28
      • 2021-06-12
      • 2021-10-13
      • 2019-01-02
      • 2021-03-14
      相关资源
      最近更新 更多