Xpath表达式根据子节点的存在选择节点? [英] Xpath Expression to select nodes based on presence of child node?
问题描述
我正在尝试使用 Xpath 查找具有给定 CSS 类 (.product) 的所有
<a href="#product/1966740"><img class="cover_image" src="https://foobar.JPG" alt="foobar" title="foobar"><h3>foobar</h3></a><div class="price">$66.00</div><div class="rrp">$219.11</div>
所以找到这些的 Xpath 相对简单:
//div[@class="product"]
但是,我只想找到那些没有售罄的产品.售罄的产品如下所示:
<img class="availability" src="https://sold_out_tag.png" alt="sold out"><a href="#product/1963553"><img class="cover_image" src="foobar.jpg" alt="foobar" title="foobar"><h3>FooBar<h3></a><div class="price">$40.00</div><div class="rrp">$129.33</div>
我想我可以使用 not(),并检查
并将 alt 属性设置为售罄".但是,以下内容无效:
//div[@class="product" and not([img[@alt="sold out"]])]
关于如何让它工作的任何想法?
干杯,维克多
您就快到了.您不需要 []
在 img
子路径周围:
//div[@class="product" and not(img[@alt = "sold out"])]
另一种写基本相同的东西的方法是:
//div[@class="product" and not(img/@alt = "sold out")]
最后,由于 class
属性可以包含多个类,通常最好使用 contains()
来检查类:
//div[contains(concat(' ', @class, ' '), " product ") 而不是(img/@alt = "sold out")]
I'm trying to use Xpath to find all <div>
s with a given CSS class (.product).
<div class="product">
<a href="#product/1966740"><img class="cover_image" src="https://foobar.JPG" alt="foobar" title="foobar">
<h3>foobar</h3></a>
<div class="price">$66.00</div>
<div class="rrp">$219.11</div>
</div>
So the Xpath to find those is relatively simple:
//div[@class="product"]
However, I only want to find those products that aren't sold out. Sold out products look like this:
<div class="product">
<img class="availability" src="https://sold_out_tag.png" alt="sold out">
<a href="#product/1963553"><img class="cover_image" src="foobar.jpg" alt="foobar" title="foobar">
<h3>FooBar<h3></a>
<div class="price">$40.00</div>
<div class="rrp">$129.33</div>
</div>
I was thinking I can use not(), and check for <div>
s that do not contain an <img>
with the alt attribute set to "sold out".
However, the following isn't valid:
//div[@class="product" and not([img[@alt="sold out"]])]
Any thoughts on how to get this working?
Cheers, Victor
You were almost there. You don't need []
s around the img
sub-path:
//div[@class="product" and not(img[@alt = "sold out"])]
Another way to write essentially the same thing is:
//div[@class="product" and not(img/@alt = "sold out")]
Lastly, since class
attributes can contain more than one class, it's often better to use contains()
to check for classes:
//div[contains(concat(' ', @class, ' '), " product ") and not(img/@alt = "sold out")]
这篇关于Xpath表达式根据子节点的存在选择节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!