html - 如何在 xpath 中链接包含和不包含?

标签 html css xpath

如何在 xpath 中链接 contains 和“not-contains”?

我想确保按钮具有 add-to-cart-button 类,并且不具有 btn--disabled 类。

我该怎么做?这是我到目前为止所拥有的:

button[contains(@class, "add-to-cart-button")]

编辑:在我的元素中,我有一个产品列表。现在我想选择页面上第一个没有类 btn--disabled

的 Article-Container

这是 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/

相关文章:

html - 仅使用纯 CSS 自动调整输入宽度

javascript - 从 bootstrap CDN 更改 dateTimepicker 的格式

javascript - 显示特定 URL 的 DIV

c# - .NET : How do you remove a specific node from an XMLDocument using XPATH?

php - Xpath php 获取链接

JavaScript 初学者 : why does this not work?

html - float Divs代替表格实现布局

css - 具有流体高度的 anchor 的文本垂直居中

javascript - jQuery/Bootstrap : Child DIV opens up Parent Modal

javascript - 使用 Selenium 在 <p> 标签内的特定单词前单击