![](/img/trans.png)
[英]Troubleshooting XPath Expression to select nodes based on child node
[英]Xpath Expression to select nodes based on presence of child node?
我正在嘗試使用Xpath查找具有給定CSS類(.product)的所有<div>
。
<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>
因此,找到這些的Xpath相對簡單:
//div[@class="product"]
但是,我只想找到那些沒有售完的產品。 售罄的產品如下所示:
<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>
我以為我可以使用not(),並檢查不包含alt屬性設置為“售罄”的<img>
<div>
。
但是,以下無效:
//div[@class="product" and not([img[@alt="sold out"]])]
關於如何使它起作用的任何想法?
干杯,維克多
使用此XPath:
//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 ") and not(img/@alt = "sold out")]
你在犯一個小錯誤,應該是
// div [@ class =“ product”] / img [@alt!=“售罄”]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.