Is it possible for one XPath expression to match all the following <a>
elements using the text in the element, in this case "Link"
?
Examples:
<a href="blah">Link</a>
<a href="blah"><span>Link</span></a>
<a href="blah"><div>Link</div></a>
<a href="blah"><div><span>Link</span></div></a>
This simple XPath expression,
//a[contains(., 'Link')]
will select the a
elements of all of your examples because .
represents the current node (a
), and contains()
will check the string value of a
to see if it contains 'Link'
. The string value of a
already conveniently abstracts away from any descendent elements.
This even simpler XPath expression,
//a[. = 'Link']
will also select the a
elements in all of your examples. It's appropriate to use if the string value of a
will exactly equal, rather than just contain, "Link"
.
Note: The above expressions will also select <a href="blah">Li<br/>nk</a>
, which may or may not be desirable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With