【问题标题】:How to write a XPath using the > Operators?如何使用 > 运算符编写 XPath?
【发布时间】:2018-12-26 12:53:18
【问题描述】:

我正在尝试使用以下 XPath 将价格为 >(大于)35 美元的商品添加到购物车

//div[@class='m-product-mini']//span[contains(text()>'$35.00')] 

但是使用这个 XPath 我无法识别价格值,下面是 HTML 代码。

<div class="m-product-mini">
    
    <div data-id="EF_TLR04-1A-P_EF_TLR04-1A">
        <!-- main-image -->
        <div class="m-product-mini-image">
            <a href="#" class="btn btn-light btn-quickview no-mobile" style="opacity: 0;">Quick view</a> 
                <a href="/bouquet/stunning-statement-bouquet/p_ef_tlr04-1a?skuId=EF_TLR04-1A&amp;zipMin=">
            </a>
        </div>
         
             <span class="m-product-mini-merchandising-icon">
                 <img src="new.jpg" alt="New Flower Arrangement by Florence's Flowers &amp; Gifts">
             </span>
          
        <a href="/bouquet/stunning-statement-bouquet/p_ef_tlr04-1a?skuId=EF_TLR04-1A&amp;zipMin=" class="m-category-flower-link centered text-large"><h2 class="m-category-flower-link-h2">Stunning Statement Bouquet</h2></a>
                 
        
        <a href="/bouquet/stunning-statement-bouquet/p_ef_tlr04-1a?skuId=EF_TLR04-1A&amp;zipMin=" class="m-product-mini-price "><span>$36.99</span> <span class="priceTag-discount"></span></a>
    
    </div>
    </div>

【问题讨论】:

    标签: selenium selenium-webdriver xpath operators


    【解决方案1】:

    尝试使用下面的 XPath 来获得所需的输出:

    //div[@class='m-product-mini']//span[number(substring-after(text(), '$')) > 35] 
    

    请注意,您需要

    • 去掉"$"符号,所以使用substring-after(text(), '$')
    • 将结果转换为整数,所以使用number()

    【讨论】:

    • 谢谢,现在我可以确定价格了
    • 需要一个额外的输入如何在 2 个产品价格相同时跳过产品,即在产品列表中有 10 个产品和 10 个中的 2 个价格相同,因此在添加到购物车时如何使用此 xpath在添加到购物车时跳过重复(相同价格)的产品,请帮助我
    • 我认为你应该使用 findElements 并使用 for 循环...对它们...你怎么看?
    • @user3538483 ,您可能需要使用编程语言功能来过滤项目。您使用的是哪种 Selenium 绑定?
    • List priceSpans = getDriver().findElements(By.xpath("//div[@class='m-product-mini']//a//span[(contains( text(),'$')) 和 not(包含(@class,'priceTag-discount'))]")); List priceOfProducts = new ArrayList(); for (WebElement webElement : priceSpans) { String priceText = webElement.getText(); if (priceText != null && priceText.length() > 0) { Double priceValue = Double.parseDouble(priceText.replace('$', ' ').trim()); priceOfProducts.add(priceValue); System.out.println("PLP 产品价格为:"+ priceValue); } }
    猜你喜欢
    • 2016-11-07
    • 1970-01-01
    • 2018-06-27
    • 2021-08-27
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-08-10
    • 2013-03-31
    相关资源
    最近更新 更多