mfeingold
mfeingold

Reputation: 7154

Selenium xpath selector based on the element text

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.

Upvotes: 26

Views: 80646

Answers (4)

Pratik Patel
Pratik Patel

Reputation: 2443

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

Upvotes: 5

Hai Nguyen
Hai Nguyen

Reputation: 1823

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

Upvotes: 7

Adnan Ghaffar
Adnan Ghaffar

Reputation: 1423

If you want to get by text

[.= 'Second']

or

[text() = 'Second']

Upvotes: 7

Amey
Amey

Reputation: 8548

I think this is what you are looking for

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

and better still

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

Upvotes: 46

Related Questions