I would like to locate this element,
<div class="item-price">$0.99</div>
using XPath which says select div
elements whose class
attribute equals "item-price"
and whose content contains a dollar sign ($
).
I would like to locate this element,
<div class="item-price">$0.99</div>
using XPath which says select div
elements whose class
attribute equals "item-price"
and whose content contains a dollar sign ($
).
This XPath expression:
//div[contains(@class, 'item-price') and contains(., '$')]
Will match all div
elements of the item-price
class containing a '$'.
It's useful to use contains()
in the test on @class
if you want to match cases where there are multiple CSS styles specified in the @class
value.
Caution: For a more robust solution, apply the following technique to avoid unintended substring matches (item-price
matching, say, item-prices
):
//div[contains(concat(' ',@class,' '), ' item-price ') and contains(., '$')]
As per the HTML:
<div class="item-price">$0.99</div>
There is:
item-price
$
sign.So, to locate this element with respect to the value of the class attribute i.e. item-price
and innerText content starting with a dollar sign ($
) you can use either of the following solutions:
Using starts-with()
:
//div[@class='item-price' and starts-with(., '$')]
Using contains()
:
//div[@class='item-price' and contains(., '$')]