is the list returned by findElements() guaranteed to be in order?

1,044 views
Skip to first unread message

Mikhail Ramendik

unread,
Apr 25, 2017, 10:19:44 AM4/25/17
to Selenium Users
Hello,

Let's say we have a simple list on a webpage:

<ul id="mylist">
   <li>item 1</li>
   <li>item 2</li>
   <li>item 3</li>
</ul>

and we find the list of the <li> items: (Java)

WebElement ul = driver.findElement(By.id("mylist"));
WebElement lis = ul.findElements(By.tagName("li"));

Is it guaranteed that the elements in lis are in the same order as they are on the webpage? For xample, will li.get(1).getText() always return "Item 2" in this and future versions of Selenum WebDriver?

If not, then are there other reliable ways of getting "the second li element" apart from using xpath?

Thanks!

Yours, Mikhail Ramendik

Anish Pillai

unread,
Apr 25, 2017, 8:51:41 PM4/25/17
to Selenium Users
Hi Mikhail,

In Java, it depends on what type of Collections you use. If you use list, it will show you the results in the same way they were inseted in the list. Then you can use Set, which would sort the records

        Set<String> set = new TreeSet<String>();
        set.add("item 2");
        set.add("item 1");
        set.add("item 3");
       
        for (String s : set) {
            System.out.println(s);  //This returns the result as item 1, then item 2 and item 3
        }
       
        List<String> list = new ArrayList<String>();
        list.add("item 2");
        list.add("item 1");
        list.add("item 3");
       
        for (String s : list) {
            System.out.println(s); //This returns the result as item 2, item 1 and item 3
        }

So if you use List<WebElement>, then the order would be preserved. A better approach would be to check the value by iterating through the list first. Example:

List<WebElement> list = driver.findElements(By.xpath(//ul[@id = 'mylist']/li))

//Iterate through the list
for(WebElement element : list) {
  if(element.getText().equals("item 2") {
    System.out.println("correct item found");
  }
}



Cheers, Anish
AutomationTestingHub

SuRiNdEr Komire

unread,
Apr 26, 2017, 6:56:26 AM4/26/17
to Selenium Users
Hi Mikhial,

As mentioned by Anish, it depends on what type of Collections you use.Like map, list, array etc. If you use Map the order would be same as inserted, it will vary, if it is list, it will show you the results in the same way they were inserted in the list. There are many other ways to store and iterate values based on our requirement.
As per your question, when you use findElements, it will fetch the values from UI as they appear in the HTML code. It will give you result in order as they appear on the page.

Ex: item1, item2, item3
But the result will depend on the Java collection you use.

Cory K

unread,
Apr 28, 2017, 1:04:49 PM4/28/17
to Selenium Users
Hi Mikhail,

Good question. I tried to look it up here and here, as you probably did, and find no mention of order. So I asked an expert I know and they said the order will be in "tab order" which is "the order in which a CSS selector works." So I guess that would be defined in the css or xpath selector spec.

I hope that helps. 

Cheers,
Cory
Reply all
Reply to author
Forward
0 new messages