Selenium iframe数据发布python

2024-04-19 17:32:39 发布

您现在位置: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"}

Tags: 数据httpscomdata部件wwwfeedelement
1条回答
网友
1楼 · 发布于 2024-04-19 17:32:39

这里实际上有两个嵌套的iframe。再加上一些WebDriverWait,我得到了下面的解决方案。在这里,我找到“Instrument”字段并将文本输入其中:

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:\chromedriver\chromedriver.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()

相关问题 更多 >