If there are two products with the same price for 499 RS, If I want to select the second one, my code always selects the 1st product, it is not selecting the second product, for this kind of duplicate items what is the solution ?
Asked
Active
Viewed 2,346 times
-3
-
Second element can be fetched by [2] – Dev Anand Sadasivam Jan 19 '16 at 04:10
-
Guys am unable to post that HTML code, so the steps to replicate the above scenario is Flipkart.com --> Casual shoes for men --> check the radio button for rs499 and below --> select the 4th shoe by using rs499 Property name. – Cheenu A Jan 19 '16 at 04:10
-
use and index e.g. //*[@id="question"]/table/tbody/tr[1]/td[2]/div/div[1]/p – PauAI Jan 19 '16 at 04:15
-
1For an element attribute cannot be duplicated, across elements if it have same set of value, then do comparison and over that do your transformation. If path is not uniformly dispersed then use `//elt[@price]`. `//elt` fetches all the element with the name `elt` from the root. I hope `//` can be taken into the mid of any typical `xpath` – Dev Anand Sadasivam Jan 19 '16 at 04:24
-
`xpath` includes comparisons also (`expression`), when it's been done either it returns node(s), value(s), counts .., else `true` or `false` in case of `test='xpath-comparison'`....... if you need `attribute` name,- http://stackoverflow.com/questions/694298/xpath-1-query-and-attributes-name And if you need `node`name,- http://stackoverflow.com/questions/7984508/getting-elements-name-in-xpath – Dev Anand Sadasivam Jan 20 '16 at 04:20
2 Answers
0
Here is how you can construct your xpath
for any of the check box item.
.//ul[@id="price_range"]//li[contains(@title,'1500')]//input
If you change the number '1500' with what ever listed there, you can select that. you can write a function to parameterize this. For example.
public void selectPriceTag(String priceAsString){
WebElement prigeRange = driver.findElement(By.id("price_range"))
.findElement(
By.xpath(".//li[contains(@title,'"+priceAsString+"')]//input"));
priceRange.click();
}