26

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 Answers 4

46

I think this is what you are looking for

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

and better still

ul/li[text() = 'Second']
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I am confused though why li[text()="Second"] does not work?
You know what - it does. Thank you again
7

If you want to get by text

[.= 'Second']

or

[text() = 'Second']

Comments

7
By.xpath( "//li[contains(text(), 'Second')]" )

Comments

5

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")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.