1

I m trying to build a selenium automation. But i can't pass this problem :

I am trying to click an icon called Data inside of a webpage, but when adress the element, the code clicks another element.

This is the html source of the element:

<a id="EPM_CA_3_3" class="app-nav-label top-nav-label" tabindex="0">Data</a>

When the code is executed it clicks another element.

This is the html source of the WRONG element:

<a id="EPM_CA_2_0" class="app-nav-label top-nav-label" tabindex="0">Tasks</a>

And here is screenshot of the elements (correct and wrong one) elements of the screen

As you can see that both of element has different text even so the code runs incorrectly.

And here is my python code:

try:
    elementinside = '//a[text() ="Data"]' 
    databutton = WebDriverWait(self.driver, 10).until(
        EC.presence_of_element_located((By.XPATH, elementinside)))
    element = self.driver.find_element(By.XPATH,elementinside)
    # element.click()
    actions = ActionChains(self.driver)
    actions.move_to_element(element).click().perform()


except TimeoutException:
    print("signing in fail!")

I couldn't find out any answer from internet. Please assist.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
mete BEDER
  • 41
  • 3
  • can you share a link to that page? – Prophet Feb 15 '22 at 16:55
  • if you are using chrome, and inspecting the element; try getting the FULL xpath of the object. based on what you have described it seems like you are clicking on the first item in an array of items which may mean you need to specify more clearly which item you intend to click on; getting the full xpath may help with this. – Shawn Ramirez Feb 15 '22 at 20:27

1 Answers1

0

The desired element is a dynamic element, so to click() on the clickable element instead of presence_of_element_located() you need to induceWebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using LINK_TEXT:

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

    WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='app-nav-label top-nav-label' and 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352