1

As per the below code, I navigate to a specified URL and select the values in the from and to field. The xpaths mentioned in the 3rd and 4th line, each returns 2 instances. In the third line, findElement selected the first instance. But in the fourth line, the findElement method selected the second instance. As per my understanding, findElement method will always select the first instance.

So, is there any specific xpath logic which caused it to select the second instance or what is the difference between 4th and 5th line in this context ?

Below is the code:

driver.get("https://www.spicejet.com");
driver.findElement(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
driver.findElement(By.xpath("//a[@value='GOI']")).click();
driver.findElement(By.xpath("//a[@value='DEL']")).click();
driver.findElement(By.xpath("(//a[@value='DEL'])[2]")).click();

Appreciate your help on this.

2
  • Are you sure that what's rendered first on the page is the first in the DOM?
    – ernie
    Mar 8, 2019 at 1:16
  • @ernie: I selected some value in the FROM field, the drop-down of the TO field automatically shows, now run the $x("//a[@value='DEL']") query in the Console tab, it shows an array, in which first element highlights nothing and second element highlights the element inside the TO field Mar 9, 2019 at 16:45

3 Answers 3

0

Q- Difference between 4th and 5th line

driver.findElement(By.xpath("//a[@value='DEL']")).click();
driver.findElement(By.xpath("(//a[@value='DEL'])[2]")).click();

-- Internally findelement() calls findelements() and return always first instance from array.

However your xpath //a[@value='DEL' is returning more values and [2] denotes 3 rd value from the list of matching elements. 4th line should always return the first element from the matching elements list. 5th Line should return a 3rd matching element from the list. Hope this helps you.

1
  • There are some mistakes in your answers. [2] denotes second element because the index starts from 1, not 0. My question is why did findElement method of the 4th line selected the second instance and not the first instance. Hope the question is clear. Mar 8, 2019 at 11:18
0

In normal circumstances (on a static page) XPath locators will consistently return the same element.

I can't say for sure without a lot more investigation into that page itself, but I would expect the DOM is being dynamically changed after the click, and this is altering what is being returned by the same XPath query.

To stop this happening, you could be more specific about which element you want it to click, using the ID of the containing DIV as a starting point.

For example, this would locate DEL but only in the destination list:

By.xpath("//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[@value='DEL']");
2
  • Even if i just mention "//a[@value='DEL']", still it highlights the same element as would be highlighted by your xpath. To be more precise, I selected some value in the FROM field, the drop-down of the TO field automatically shows, now run the $x("//a[@value='DEL']") query in the Console tab, it shows an array, in which first element highlights nothing and second element highlights the element inside the TO field. So, my query is why did the findElement() method selected the second element from the above array. Mar 9, 2019 at 16:55
  • I'm not sure that it did. What language are you running? In Java I see behaviour consistent with another element with the same locator being created during interaction with the page. For the plain @value=DEL query, I consistently get an element at the following location (419, 574) for the original click, and at the final point in my test. For the [2] query the element is at (674, 548). They're different elements, even if they have the same effect in your test somehow.
    – anonygoose
    Mar 10, 2019 at 15:45
0

Understanding XPath: XPath is a way to navigate through elements in an HTML document. It's like giving directions to find a specific element on a webpage.

Behavior of findElement: When you use findElement with an XPath in Selenium, it returns the first element that matches the XPath. If there are multiple elements with the same XPath, it will select the first one it encounters in the HTML DOM (Document Object Model).

Your Code Explanation:

Third Line: driver.findElement(By.xpath("//a[@value='GOI']")).click(); This line is looking for an anchor () tag with the value 'GOI'. As per your observation, there are two such elements. Selenium selects the first one it finds in the DOM.

Fourth and Fifth Line: These lines are a bit tricky because they are almost the same, but with a crucial difference.

Fourth Line: driver.findElement(By.xpath("//a[@value='DEL']")).click(); This line is similar to the third line but for 'DEL'. Again, there are two such elements, and Selenium selects the first one.

Fifth Line: driver.findElement(By.xpath("(//a[@value='DEL'])[2]")).click(); This line also looks for 'DEL', but notice the [2] at the end of the XPath. This is the key difference. The [2] tells Selenium to select the second instance of the element matching the XPath, not the first. So, even though 'DEL' appears twice, this line specifically asks for the second instance.

Conclusion: The reason the fourth line selects the first instance and the fifth line selects the second instance is entirely due to the XPath used. By adding [2], you instruct Selenium to pick the second matching element instead of the default first one.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.