Home > Enterprise >  Selenium [python] not clicking an <li> button
Selenium [python] not clicking an <li> button

Time:10-17

So I have been trying to use selenium to click an <li> button on an HTML which is in a <ul> tag and looks something like this:

...
 <ul id="ulVisualization">
     <li class="active" id="liMap">Map</li>
     <li id="liBar" class="">Bar</li>
     <li id="liLine">Line</li>
 </ul>
...

I have been using the following command to get to the element using XPATH:

WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH,"//li[@id='liBar']"))).click()

Yet for some reason that I have not been able to identify, this command is not able to find/click that button and the command hits its time-out.
I have even tried:

driver.find_element_by_xpath('//li[@id="liBar"]').click()

But that too was of no avail, throwing an error message saying NoSuchElementException: Message:

I would appreciate any and all help and thank you very much in advance.

EDIT:

Additionally, I have noticed that when I click on the button manually the HTML modifies to:

...
 <ul id="ulVisualization">
     <li class="" id="liMap">Map</li>
     <li id="liBar" class="Active">Bar</li>
     <li id="liLine">Line</li>
 </ul>
...

Revealing the data I am trying to acquire later in the HTML code, which was not available before I clicked the button manually.

CodePudding user response:

Since you want to click on the Element, try element_to_be_clickable, instead of presence_of_element_located

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//li[@id='liBar']"))).click()

Make sure the locator you are using is Unique, that is 1/1. Link to Refer

And also check if the Elements are in an iframe or in an shadow-root or Trying to find Element in a newly opened browser tab.

If none of them work, apply some time.sleep() and check if it works.

  • Related