Select an item from the selenium drop-down list

advertisements

I want to select an item from a dropdown using selenium. The project uses jsf, primefaces, spring. The dropdown html appears in a list. Like.

<div id="mydropdown">
   <ul>
      <li>03<li>
      <li>04</li>
      <li>23</li>
      <li>24</li>
      <li>30</li>
   </ul>
</div>

Since i want to select a random value inside of hardcoding the nth <li> to be selected, I wrote the code as

String selectItem ="03";
driver.findElement(By.xpath("//div/ul/li[contains(.,'"+selectItem+"')])"));

But the output is:

[ <li>03<li>,
  <li>23</li>,
  <li>30</li> ]

It selects all <li> items containing 3. I does not consider the zero before the 3.

My Question is: How do get the output like [ <li> 03 </li> ] only?


Check for an exact match:

driver.findElement(By.xpath("//div/ul/li[.='" + selectItem + "']"));