XPath text() expression that contains a new line
Let's say 开发者_Go百科I have the following HTML code:
<a href="/site/somesite/">
somesite</a>
My question is how can I write an XPath expression that must use the text()
property to match the somesite
link and I cannot change the source?
I'm not sure whether you want to lookup the URL based on the link text, or the link text based on the URL. This will get you the URL:
//a[normalize-space() = 'somesite']/@href
This will get you the text:
normalize-space(//a[@href = '/site/somesite/'])
Use normalize-space()
, which will throw away the leading and trailing whitespace characters(and condense repeating spaces in the middle of the text into a single space), so that you can compare the normalized text()
and use to filter in a predicate.
a[normalize-space(text())='somesite']
精彩评论