如何在 xpath 中链接 contains
和“not-contains”?
我想确保按钮具有 add-to-cart-button
类,并且不具有 btn--disabled
类。
我该怎么做?这是我到目前为止所拥有的:
button[contains(@class, "add-to-cart-button")]
编辑:在我的元素中,我有一个产品列表。现在我想选择页面上第一个没有类 btn--disabled
这是 HTML
<main>
<div class="grid shop-list__results offer-tiles">
<div class="offer-tiles__item offer-tiles--odd offer-tiles--top-border">
<article itemscope="itemscope" itemtype="http://schema.org/Product" class="offer-tile">
<div itemprop="offers" itemscope="itemscope" itemtype="http://schema.org/Offer" class="offer-tile__content">
<ul class="offer-tile__actions">
<li class="offer-tile__action offer-tile__action--add-to-cart">
<button type="button" class="btn add-to-cart-button btn--disabled">Cart</button></li>
</ul>
</div>
</article>
</div>
<div class="offer-tiles__item offer-tiles--even offer-tiles--top-border">
<article itemscope="itemscope" itemtype="http://schema.org/Product" class="offer-tile">
<div itemprop="offers" itemscope="itemscope" itemtype="http://schema.org/Offer" class="offer-tile__content">
<ul class="offer-tile__actions">
<li class="offer-tile__action offer-tile__action--add-to-cart">
<button type="button" class="btn add-to-cart-button">Cart</button></li>
</ul>
</div>
</article>
</div>
这是我到目前为止的 xpath 选择器(这显然是错误的):
//main//article[contains(descendant::button/@class, "add-to-cart-button")][not(descendant::button/@disabled)]
最佳答案
试试这个:
button[contains(@class, "add-to-cart-button") and not(contains(@class, "btn-disabled"))]
地点:
and
是两个语句之间的运算符。示例//div[x 和 y]
或//div[x 或 y]
not()
与函数中的语句相反。示例//div[x 和 not(y)]
编辑:
根据您提供的 HTML block ,您可以使用此 xPath:
//button[@class = 'btn add-to-cart-button']
或者,如果有很多添加到购物车按钮
,您可以使用如下内容:
//div[@class = 'offer-tiles__item offer-tiles--even offer-tiles--top-border']/article[@itemtype = 'http://schema.org/Product']/div[@itemtype = 'http://schema.org/Offer']//button[@class = 'btn add-to-cart-button']
关于html - 如何在 xpath 中链接包含和不包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165442/