1
votes

Can anyone help me to derive the xpath for the below HTML. I am new to selenium.

I need to get rupees for each span but the problem is both the element have same class names. How do I create unique xpath to find the elements

<div class="pu-price">
<div class="pu-border-top">
<div class="pu-final">
<span class="fk-font-17 fk-bold">Rs. 5,557</span>
</div>
<div class="pu-emi fk-font-12">EMI from Rs. 270 </div>

<div class="pu-price">
<div class="pu-border-top">
<div class="pu-final">
<span class="fk-font-17 fk-bold">Rs. 9,997</span>
</div>
<div class="pu-emi fk-font-12">EMI from Rs. 500 </div>
2
What have you tried so far? And if I remember XPath correctly you can just count through same objects. - bish
i manged to get the first price wd.findElement(By.xpath("//div[@class='pu-final']/span[1]")).getText().replace("Rs."," "); how do i count objects? - Jhonsamuel Samuel

2 Answers

2
votes

my suggestion is to use xpath that is more general to get all the elements that contains your span, for example: xpath: "//div[@class='pu-final']/" or if u want to be more specific: xpath: //div[@class='pu-final' and contains(.,'Rs')] this will bring u all the elements in the page that contains Rs and u can put them in some list:

public static List<WebElement> getElements(String selector) {
        By locator = By.xpath(selector);
        List<WebElement> webElements = driver.findElements(locator);
        return webElements;
    }

then u can do whatever u want on the list u have.

2
votes

You could use

(//div[@class = 'pu-final']/span)[1]

to get the first one, and

(//div[@class = 'pu-final']/span)[2]

for the second. This is probably what bish meant about counting through the selected nodes.