Hemanth S R
Hemanth S R

Reputation: 1113

XPath - not able to find an element with children text

<a>
    This is <var>Me</var> and That is <var> You</var>
</a>

I can find an element "a" which contains "This is" by following code:

//a[contains(text(),'This is')]

But I am not able to find element "a" which contains "This is Me and That is You".

//a[contains(text(),'This is Me and That is You')]

Is there a way to find an element with children text as well?

Upvotes: 0

Views: 157

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

This also can be find using normalize-space() function of xpath which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as below :-

//a[normalize-space()='This is Me and That is You']

Upvotes: 1

Runcorn
Runcorn

Reputation: 5226

I am not sure if this what you need but you can use string() to get the result as required,

//a[string()='This is Me and That is You']

The caveat however will be that you need to have precised information about the String being used.

See working example here.

Upvotes: 3

Related Questions