Home > other >  How to identify this element using selenium and Python?
How to identify this element using selenium and Python?

Time:02-20

I have tried to select the element below using xpath, id, class, nothing is working. This is the error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ui-id-26"]"}

HTML code of the element:

<a title="" href="#8eef8ef4-7cfc-4cb6-69cf-1cabf9625d74"  role="presentation" tabindex="-1" id="ui-id-26">Data</a>

Snapshot of the element:

the element I want to select and eventually click

I am using python.

This is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
chromedriver = 'C:/Users/misha/Downloads/chromedriver_win32/chromedriver'

driver = webdriver.Chrome(chromedriver)
driver.get("https://login.cmegroup.com/sso/accountstatus/showAuth.action")
driver.maximize_window()
login = driver.find_element(By.ID, "loginBtn")

name = driver.find_element(By.ID, "user")
name.send_keys("MyEmail")
pwd = driver.find_element(By.ID, "pwd")
pwd.send_keys("MyPassword")
login.click()

time.sleep(15)

driver.get('https://www.cmegroup.com/trading/fx/cme-fx-market-profile-tool.html#analyze-more')


data = driver.find_element(By.XPATH,'//*[@id="ui-id-26"]')

data.click()


CodePudding user response:

To click on the element with text as Data you can use either of the following locator strategies:

  • Using link_text:

    driver.find_element(By.LINK_TEXT, "Data").click()
    
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']").click()
    

The desired element is a dynamic element, so ideally to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using LINK_TEXT:

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

  • Related