故障排除XPath表达式以根据子节点选择节点



nb-这个问题与我问的另一个问题非常相似-xPath表达式以基于子节点的存在选择节点吗? - 但是,我正在尝试扩展它并失败。

我有一个HTML页面清单产品。

我正在尝试使用XPath区分可用的产品和售罄的产品。

可用产品看起来像这样:

<div class="product-widget-container">
    <article itemscope="" itemtype="http://schema.org/Product" class="product grid_4 full space omega large " data-productid="1996364" data-name="Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml" data-actual-price="5.99" data-is-available="true" data-low-stock="" data-popularity="6" data-smallimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-largeimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-sizes="[]" data-available-sizes="[]" data-categories="[119977]" data-brand="That Natural Source" data-discount="83" data-default-order="9">
        <figure>
            <div class="product-img-container ">
            <img itemprop="image" class="lazy product-img" src="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" data-original="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996364_94d4a520-7e4a-11e3-930f-000c29c9a057_image_310x434.JPG" alt="Up to 85% off Summer Looks Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml " style="display: inline;">
            <span class="arrow arrow-up"></span>
            <div class="quick-buy" style="display: none;">
            <span class="arrow-down-trans"></span>
            <div class="select-size">
            <form class="express-buy" action="/basket/add/1996364/" method="post">
            <input type="hidden" id="id_quantity_1996364" class="purchase-quantity" name="quantity" value="1">
            <input type="hidden" value="" name="addbasket.x">
            <span>
            <input class="add-to-basket btn btn-primary  btn-large " type="submit" value="ADD TO BASKET">
            </span>
            </form>
            </div>
            </div>
            </div>
            <a itemprop="url" class="overlay-link" href="/event/outlet/up-to-off-summer-looks/1996364-daily-wrinkle-defence-essential-skin-reviver-cream-cleanser-100ml/" title="Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml"></a>
            <figcaption>
            <h2 itemprop="name" class="mason name">
            That Natural Source: Daily Wrinkle Defence Essential Skin Reviver Cream Cleanser - 100ml
            </h2>
            <small itemprop="brand" class="bed"> Up to 85% off Summer Looks</small>
            <small class="bed shoes-price">
            $5.99
            <del>$34.95 RRP</del>
            <span class="discount">(83% discount)</span>
            </small>
            </figcaption>
        </figure>
    </article>
</div>

售罄的产品看起来像这样:

<div class="product-widget-container">
    <article itemscope="" itemtype="http://schema.org/Product" class="product grid_4 full space omega large " data-productid="1996526" data-name="#T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI" data-actual-price="10.99" data-is-available="" data-low-stock="true" data-popularity="1" data-smallimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-largeimgsrc="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-sizes="[]" data-available-sizes="[]" data-categories="[119968]" data-brand="OPI" data-discount="0" data-default-order="39">
        <div class="stock-status be_sprites sold-out">Sold Out</div>
        <figure>
            <div class="product-img-container ">
            <img itemprop="image" class="lazy product-img" src="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" data-original="https://staging.foo.com.au/site_media/uploads/product_image/2014/1/16/pd1996526_d0402efe-7e4a-11e3-930f-000c29c9a057_image_310x434.jpg" alt="Up to 85% off Summer Looks #T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI " style="display: inline;">
            <span class="arrow arrow-up"></span>
            </div>
            <a itemprop="url" class="overlay-link" href="/event/outlet/up-to-off-summer-looks/1996526-t58-when-monkeys-fly-oz-the-great-and-powerful-collection-by-opi/" title="#T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI"></a>
            <figcaption>
            <h2 itemprop="name" class="mason name">
            Opi: #T58 When Monkeys Fly! - Oz The Great And Powerful Collection By OPI
            </h2>
            <small itemprop="brand" class="bed"> Up to 85% off Summer Looks</small>
            <small class="bed shoes-price">
            $10.99
            </small>
            </figcaption>
        </figure>
    </article>
</div>  

我以为我可以上它上的"售罄"类或其中的Sold Out文本。

我已经尝试了所有以下所有操作,但它们似乎都没有用 - 它们都给了我全套产品:

//div[@class="product-widget-container" and not(div[@class="stock-status be_sprites sold-out"])]
//div[@class="product-widget-container" and not(div[contains(@class, "sold-out")])]
//div[@class="product-widget-container" and not(div[contains(., "Sold Out")])]

对我在XPath表达中做错了什么的想法?

欢呼,Victor

您的表达式有正确的想法,但是您不需要嵌套[ ]括号。打开它们后,您将处于有条件的语句中:您编写的所有内容都将成为该声明的一部分。因此,当您想检查子节点的属性时,只需选择它: node[child/@attribute]

您还需要在任何深度检查div,因为它不是第一个子节点。如果您编写div[div/@class="foo"],则将检查<div><div class="foo"></div></div>。如果您编写div[.//div/@class="foo"],则将检查<div><anything><bar><div class="foo"></div></bar></anything></div>

之类的东西
//div[@class="product-widget-container" and not(.//div/@class="stock-status be_sprites sold-out")]

应该工作!

尝试

//div[@class='product-widget-container' and not(@class='stock-status be_sprites sold-out')]

您应该在谓词

中删除div[]