XPath to match @class value and element value?

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(., '$')]

Leave a Comment