Home > Software engineering >  How to choose a value from a list in Selenium using Python?
How to choose a value from a list in Selenium using Python?

Time:01-13

I have a list of this form:

<span >
<i ></i>
<span>&ensp;1000 rows&emsp;</span></span>

<div id="tool-row-menu"  tabindex="-1" style="transform-origin: left top; left: 0px; top: 0px;">

<ul  role="menu" aria-hidden="true">
<li data-value="5"  role="menuitem" tabindex="0">5</li>
<li data-value="20"  role="menuitem" tabindex="0">20</li>
<li data-value="100"  role="menuitem" tabindex="0">100</li>
<li data-value="500"  role="menuitem" tabindex="0">500</li>
<li data-value="1000"  role="menuitem" tabindex="0">1000</li>
<li data-value="2000"  role="menuitem" tabindex="0">2000</li>
<li data-value="5000"  role="menuitem" tabindex="0">5000</li>
</ul>
</div>

I have entered the drop down list but am not being able to select the option that I want: 5000.

driver.find_element_by_xpath("//span[text()=' 1000 rows ']").click() #works

driver.find_element_by_xpath("//li[text()='5000']").click()  #error

The error message : no such element: Unable to locate element: {"method":"xpath","selector":"//li[text()='5000']"}

I could only find answers for lists starting from 'select' and having 'options'. Since I am only a beginner at Selenium, I would want someone to point out the error that I am making.

CodePudding user response:

Try to find not with a text, but with another attribute. For example:

driver.find_element_by_xpath("//li[@data-value="5000"]).click()

CodePudding user response:

wait=WebDriverWait(driver,30)            
wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@data-value='5000']"))).click()

You want to add some wait for the element to popup to click.

Imports:

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