Selenium iframe Data issue - python



我一直在尝试从 https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed 访问小部件数据,以便我的程序可以抓取网站并选择搜索栏以输入数据并在iframe中返回结果。但是,即使我将驱动程序转换为 iframe,它仍然找不到 iframe 数据元素。错误:我试图添加加载所有内容的时间延迟,但仍然没有运气。

elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"d-e-Xg"}

这里实际上有两个嵌套的 iframe。结合一些WebDriverWait,我得出了以下解决方案。在这里,我找到"仪器"字段并在其中输入文本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(
    options=options, executable_path=r"C:chromedriverchromedriver.exe"
)
driver.implicitly_wait(10)
try:
    driver.get(
        "https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed"
    )
    driver.switch_to.frame(driver.find_element(By.ID, "widget-container"))
    driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))
    # Wait for "Loading" overlay to disappear
    WebDriverWait(driver, 10).until(
        ec.invisibility_of_element_located((By.CLASS_NAME, "d-e-r-k-n"))
    )
    # Click "Instrument"
    driver.find_element(By.CLASS_NAME, "d-oh-i-ph-qh-rh-m").click()
    # Enter text into now-visible instrument field
    driver.find_element(By.CLASS_NAME, "d-e-Xg").send_keys("metlife")
finally:
    driver.quit()

最新更新