在2个tds之间建立关系- Selenium Java。

问题描述 投票:0回答:1

我试图在HashMap中收集价值--名称、数量、每公斤价格和总价值。我可以使用以下两个项目来收集名称和数量。

private By productName = By.xpath("//td//p[@class='product-name']");
private By productQuantity = By.xpath("//td//p[@class='quantity']");

问题: 当我尝试/td/p[@class='金额']时,它最终选择了4。我一直在试验的几个选项是选择与产品名称相关的第二个td,这将给我每公斤的价格。//td//p[@class='product-name']/following-sibling:://td[2]//td//p[@class='product-name']/ancestor::td

都不能用.有什么线索能让我在这里用吗?先谢谢你的时间。

源。

<tr>
    <td><img class="product-image" src="./images/cucumber.jpg" style="width: 50px; height: 50px;"></td>
    <td>
        <td>
            <p class="product-name">Cucumber - 1 Kg</p>
        </td>
        <p class="quantity">2</p>
    </td>
    <td>
        <p class="amount">48</p>
    </td>
    <td>
        <p class="amount">96</p>
    </td>
</tr>
selenium html-table
1个回答
0
投票

如果你想得到96的值,可以使用下面的方法。xpath

//td[.//p[@class='product-name']]/following::td[2]/p

//td[.//p[@class='product-name']]/following-sibling::td[2]/p
© www.soinside.com 2019 - 2024. All rights reserved.