22
votes

What would a Selenium xpath selector be for the following HTML:

<ul>
   <li>First</li>
   <li>Second</li>
   <li>Third</li>
</ul>

I need to make Selenium IDE locate the second item on the list based on the element text. I thought //li='Second' would do the trick, but apparently it does not.

4

4 Answers

42
votes

I think this is what you are looking for

ul/li[contains(text(), "Second")]

and better still

ul/li[text() = 'Second']
6
votes

If you want to get by text

[.= 'Second']

or

[text() = 'Second']
5
votes
By.xpath( "//li[contains(text(), 'Second')]" )
3
votes

You can use like this:

//li[. = "Second"]

OR

//li[contains(., "Second")]

Here, contains mean that you can match the partial text, so below one is also correct:

//li[contains(., "Seco")]