Selenium iframe Data issue - python

167 views Asked by At

I have been trying to access the widget data from https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed so that my program can scrape the website and select the search bar to enter the data and return the result in the iframe. However, even when I convert my driver to the iframe it still can't find the iframe data elements. the error: I tried to add the time delay for everything to load but still no luck.

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

There are 1 answers

1
cody On BEST ANSWER

There are actually two nested iframes in play here. That, combined with some WebDriverWaits allowed me to arrive at the following solution. Here I locate the "Instrument" field and input text into it:

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