0

I have a snippet from the HTML page. I want to know how I can use selenium to click on this javascript. The actual link is https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html

<a id="ctl00_MainContent_ucViewControl_IntegratedFedWatchTool_lbPTree" href="javascript:__doPostBack('ctl00$MainContent$ucViewControl_IntegratedFedWatchTool$lbPTree','')">Probabilities</a>

I am getting this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a[id='ctl00_MainContent_ucViewControl_IntegratedFedWatchTool_lbPTree']"}

I have tried the below but no luck!

link = driver.find_element_by_css_selector("a[title='Probabilities']")[0]

link = driver.find_element_by_css_selector("a*='Probabilities'")
link.click()

I want to have selenium to be able to click on this button using Python.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
HelloWorld123
  • 79
  • 2
  • 9

3 Answers3

2

That button is in an iframe, you would need to change the focus of webdriver to frame, for that you can use this code

Code :

wait = WebDriverWait(driver,10)

driver.maximize_window()

driver.get("https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html")

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id^='cmeIframe-']")))

wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Probabilities'))).click()

You need to import these :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

Let me know if you have any more concerns.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
1

The desired element is a JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[id*='_MainContent_ucViewControl_IntegratedFedWatchTool_']"))).click()
    
  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Probabilities"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@id, '_MainContent_ucViewControl_IntegratedFedWatchTool_') and text()='Probabilities']"))).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
    
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

since your a element has id attribute you can use that to find it and click on it

 driver.find_element_by_id('ctl00_MainContent_ucViewControl_IntegratedFedWatchTool_lbPTree')
Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27