如何在同一xpath中添加两个div位置以忽略重复值?

时间:2019-01-14 08:28:09

标签: selenium selenium-webdriver xpath

在订单确认页面上,我们有2个产品,我想获得商品名称,但商品名称+附加商品标签相同,因此现在的清单是4, 我只想要商品名称 这是我的HTML代码

        <div class="grid_3 d-grid_10">
            <p class="m-confirmation-modal-print-item-title">
                Beautiful in Blue
                        (Standard)
                        </p>
        </div>
        <div class="grid_3 d-grid_10">
                    <p class="m-confirmation-modal-print-item-title">
                        2 Balloons&nbsp;
                        mylar balloons:</p>
    </div>

        <div class="grid_3 d-grid_10">
            <p class="m-confirmation-modal-print-item-title">
                Golden Glow
                        (Standard)
                        </p>
        </div>
        <div class="grid_3 d-grid_10">
                    <p class="m-confirmation-modal-print-item-title">
                        2 Balloons&nbsp;
                        mylar balloons:</p>
                </div>

1 个答案:

答案 0 :(得分:0)

使用Java,您可以简单地使用Set,其中不会包含任何重复的元素,如下所示:

List<WebElement> productElements = driver.findElements(By.xpath("//p[@class='m-confirmation-modal-print-item-title']"));
HashSet<String> productNames = new HashSet<>();
for (WebElement productElement : productElements)
{
    String productName = productElement.getAttribute("innerHTML");
    productNames.add(productName);
}
for(int i = 0; i < productNames.size(); i++) 
{
    System.out.println(productNames.get(i));
}