1

I'm trying to select the "Majors" button on [this webpage] (https://www.dukascopy.com/swiss/english/marketwatch/historical/)

I am able to switch into the iframe where this element is contained, but trying to click by using the xpath, id or class name of the element all result in a seemingly random button within the same column being selected. Below is the current code I'm using along with a screenshot of inspection of the button.

majorforexbutton = driver.find_element_by_xpath('/html/body/div[9]/div[1]/div[3]/ul/li[4]')
majorforexbutton.click()

screenshot:

screenshot

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

Try grabbing your element and trying either one of the bottom.

elem = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.ID, ":5x")))

1 of these:

elem.click()

driver.execute_script("arguments[0].click();", elem)
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

The element with text as Majors field is within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='freeserv.dukascopy.com/2.0/?path=historical_data_feed']")))
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Instrument']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li[data-group='FX_MAJORS'][data-parent='FX']"))).click()
      
    • Using XPATH:

      driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'freeserv.dukascopy.com/2.0/?path=historical_data_feed')]")))
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Instrument']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[text()='Majors']"))).click()
      
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Major


Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Did you try to click Major button manually on page ?? Please try to click it manually and see the difference . Actually your **output is wrong** as it has not clicked Major button instead it has clicked Austria under Stock (CFD) section. – rahul rai Sep 04 '20 at 10:10
  • @rahulrai Good observation. Check out the updated answer and let me know the status. – undetected Selenium Sep 04 '20 at 10:27
  • 1
    Yeah, can see from attached output major is selected. Thanks!! – rahul rai Sep 04 '20 at 10:42
  • @EliBain Feel free to visit [Selenium Chat Room](https://chat.stackoverflow.com/rooms/223360/selenium) and help other users. – undetected Selenium Oct 20 '20 at 18:35