Reputation: 1
I'm trying to match this a
link:
<a href="/collection/accessories_wall?productType=Cases+%26+Sleeves&productType=Screen+Protectors&compatibilityCategory=Phone" class="product-link" data-product-id="cases_protection" xpath="1">
Cases & protection
</a>
Tried:
//a[contains(text(),'Cases & protection')]
Upvotes: 0
Views: 35
Reputation: 29022
You probably forgot to escape the ampersand &
with &
.
So try this instead:
//a[contains(normalize-space(text()),'Cases & protection')]
EDIT:
I added the normalize-space(...)
function to only check the core text and not the surrounding spaces. Maybe that does help you.
Upvotes: 1
Reputation: 111541
What you have is fine as posted, but here are some common reasons that XPath's intending to match on element text might fail:
Additional markup such as <b></b>
thwarts tests of text()
node children.
Remedy: Test the string-value of the element:
//a[contains(.,'Cases & protection')]
Whitespace causes comparison failure.
Remedy: Use normalize-space()
:
//a[contains(normalize-space(),'Cases & protection')]
False positive matches arise because other strings match on a substring-basis:
Remedy: Test for equality rather than substring containment:
//a[normalize-space()='Cases & protection']
Webpage-related issues for unexpected failures for an XPath to match (apart from text content testing) include markup/content not having loaded yet, being dynamically generated, or residing in an iframe
.
Upvotes: 0