Selenium iframe数据问题-Python

时间:2019-01-19 23:57:15

标签: python selenium selenium-webdriver iframe

我一直在尝试从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"}

1 个答案:

答案 0 :(得分:0)

这里实际上有两个嵌套的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:\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()