Mojiz Mehdi
Mojiz Mehdi

Reputation: 205

how to get the text value of xpath?

My HTML is following:

<li>
    <span>
        <strong>13. </strong>
        "For any queries, call 123456789"
    </span>
</li

I want to make the xpath containing the text ""For any queries" to scroll my element to this particular text. I tried to make the xpath like

" //span//[contains(text(),'For any queries')] "

but it is not working.

Upvotes: 0

Views: 59

Answers (2)

E.Wiest
E.Wiest

Reputation: 5905

 //span//[contains(text(),'For any queries')]

Your XPath expression is invalid since you can't put // before a predicate [...]. Contains function will evaluate the first returned text node (in your case, a whitespace node).

You can use @Simon Tulling's answer or keep your original idea. Just remove the useless // and add a normalize-space() predicate to ignore the whitespace node (caused by the presence of the strong element in the span element).

In that case, the XPath expression will be :

//span[contains(text()[normalize-space()],'For any queries')]

Upvotes: 1

Simon Tulling
Simon Tulling

Reputation: 176

In this case since the span has multiple children text() does not properly find the text of the span. This behaviour is explained here: XPath: difference between dot and text().

To fix this particular problem the xpath could be changed to:

//span[contains(.,'For any queries')]

Upvotes: 1

Related Questions