Reputation: 11
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>
Upvotes: 1
Views: 259
Reputation: 27996
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.
Upvotes: 2
Reputation: 347
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.
Upvotes: 2