1

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
  • 1
    What have you tried so far? And if I remember XPath correctly you can just count through same objects. Commented Jun 30, 2015 at 17:46
  • 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? Commented Jun 30, 2015 at 17:51

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you and could you please share if you have any tutorial about XPATH derivation technique
the best way that i use is in the browser: open console and type $x("") between them type the xpath and u can see the results , i dont like to use the addons
2

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.

1 Comment

Yep that's what I mean :) Thank you for an example!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.