如何在xpath中链接包含和不包含的内容?

时间:2018-09-04 11:37:15

标签: html css xpath

如何在xpath中链接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)]

1 个答案:

答案 0 :(得分:2)

尝试一下:

button[contains(@class, "add-to-cart-button") and not(contains(@class, "btn-disabled"))]

其中:

  • and是两个语句之间的运算符。示例//div[x and y]//div[x or y]
  • not()是函数中语句的对立面。示例//div[x and not(y)]

编辑:

根据您提供的HTML块,您可以使用以下xPath:

//button[@class = 'btn add-to-cart-button']

或者,如果有很多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']