somesite<...">

Expression of an XPath text () expression that contains a new line

Let's say I have the following HTML code:

<a href="/site/somesite/"> somesite</a> 

My question is, how can I write an XPath expression that should use the text() property to match somesite reference, and I cannot change the source?

+6
xpath
source share
2 answers

I'm not sure if you want to search for a URL based on the link text or link text based on the URL. This will give you the url:

 //a[normalize-space() = 'somesite']/@href 

This will give you the text:

 normalize-space(//a[@href = '/site/somesite/']) 
+2
source share

Use normalize-space() , which will throw out leading and trailing white space characters (and condense repeating spaces in the middle of the text into one space) so you can compare normalized text() and use it to filter into the predicate.

 a[normalize-space(text())='somesite'] 
+2
source share

All Articles