0

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.

  • Are you sure that what's rendered first on the page is the first in the DOM? – ernie Mar 8 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 – Sandesh Sawant Mar 9 at 16:45
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.

  • 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. – Sandesh Sawant Mar 8 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']");
  • 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. – Sandesh Sawant Mar 9 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 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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