【问题标题】:Selenium iframe Data issue - pythonSelenium iframe 数据问题 - python
【发布时间】:2019-01-19 23:57:14
【问题描述】:

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

【问题讨论】:

    标签: python selenium selenium-webdriver iframe


    【解决方案1】:

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

    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()
    

    【讨论】:

    • 我有一个非常相似的代码,但对我来说主要障碍是在第二个 iframe 的搜索字段中发送文本。你知道我该怎么做吗
    猜你喜欢
    • 2021-09-10
    • 2020-10-06
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多