Sheikh Rahman
Sheikh Rahman

Reputation: 915

Create relationship between 2 tds- Selenium Java

I am trying to collect the values - name, quantity, price per KG and total in HashMap. I can use the following 2 items to collect name and quantity

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

Problem: When I try //td//p[@class='amount'] it ends up selecting 4. A few options I have been experimenting are select the 2nd td related to product name which will give me price per kg //td//p[@class='product-name']/following-sibling:://td[2] or //td//p[@class='product-name']/ancestor::td

None of them are working.Any clue what I can use here? Thanks in advance for your time.

Source:

<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>

Upvotes: 1

Views: 51

Answers (1)

KunduK
KunduK

Reputation: 33384

If you want to get value of 96 the use Following xpath

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

OR

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

Upvotes: 0

Related Questions