XPath:选择前面的<a> value if specific class is found within div

时间:2018-11-25 01:57:36

标签: xpath google-apps-script

I am new to XPath so I apologize if this is horribly easy (however couldn't find anything in search related to this specific process). I am using Xpath in google sheets to try and retrieve this information.

Example Code snippet to start:

<div class="product-grid-item card mb-4 shadow-sm">
  <a href="/collections/available/products/blue" class="card-link"></a>
  <div class="card-banner false">&nbsp;</div>
      <img src="//Blue_1_large.jpg?v=154" alt="Blue" class="card-img-top img-fluid">
  <div class="card-body pt-0">
      <div class="product-form-type mb-4 d-flex flex-direction-row flex-wrap">
        <img src="37/assets/tsf-form-icon.svg?3" alt="Flower">
            <p class="price">
<span class="range-price"><sup class="currency">$</sup>8<span class="d-none">.</span><sup>50</sup></span>
<small>/<span data-i18n="general.product.sellable_measures"></span></small></p>
    </div>
      <h4 class="card-title" data-i18n="general.product">
          Blue
      </h4>
      <p class="card-text">
          <small>8-11
              </small>
          <br>
          <small>8-11
              </small>
      </p>
  </div>
  <div class="card-footer bg-transparent">
    <hr>
  <span class="range-price"><sup class="currency">$</sup>42<span class="d-none">.</span><sup>50</sup></span>
  &nbsp;<span class="range-sep">–</span>&nbsp;
    <span class="range-price"><sup class="currency">$</sup>122<span class="d-none">.</span><sup>50</sup></span>
  <small>/<span data-i18n="general.product.sellable_measures"></span></small>
  </div>
</div>

I am trying to figure out how to pull the href value (ex. ) but I need to check to make sure that the class is equal to "card-banner false" and return the href values that pass that check.

Currently I have: //div[contains(@class,'product-grid-item card mb-4 shadow-sm') and class = 'card-link']//a/@href

However that retrieves nothing, which I believe is because I need to use preceding, however I can't seem to get that to work.

I have confirmed that it can be read as //div[contains(@class,'card-banner false')]//@* does return the proper amount of instances. I just need to find the way to reference it properly.

Thanks!

1 个答案:

答案 0 :(得分:1)

您可以使用preceding-sibling选择上一个元素

//div[contains(@class,'card-banner false')]/preceding-sibling::a/@href