简体   繁体   中英

How to write a Xpath using “not(contains())” predicate for span tag

I want to read the products which have the price and same way want to get the count of the products which are doesn't have the price. I've wrote below XPath, but it's not working

//div[@class='m-product-mini']//a[@class='m-product-mini-price ']//span[not(contains(text(),'$'))] 

HTML for element without price:

<div class="m-product-mini-image">
<a href="#" class="btn btn-light btn-quickview no-mobile" style="opacity: 0;">Quick view</a> <a class="m-product-mini-price "><span></span> <span class="priceTag-discount"></span></a>
</div>

HTML for element with price:

<div class="m-product-mini-image">
<a href="#" class="btn btn-light btn-quickview no-mobile">Quick view</a> <a class="m-product-mini-price "><span>$34.95</span> <span class="priceTag-discount"></span></a>
</div>

You've got the wrong class name at the start, your div has the class of m-product-mini-image, but your xpath is just looking for m-product-mini, the xpath:

//div[@class='m-product-mini-image']//a[@class='m-product-mini-price ']//span[not(contains(text(),'$'))]

works fine, however the issue with this is that it finds 2 span tags for each entry, as you have the span for the price which will either be empty or have the price in there, and then the following span with class=priceTag-discount in it. So on a page with both of the html snippets on it, it finds 3 elements, rather than 1. So, you either need to remove one of the span tags (Maybe have the class set in the span tag which contains the price) or you'd need to use the following xpath if that is not possible

//div[@class='m-product-mini-image']//a[@class='m-product-mini-price ']//span[not(contains(text(),'$')) and not(contains(@class,'priceTag-discount'))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM