Why does findElements return List instead of Set in Selenium?

Swaroop Nadella
2 min readMay 31, 2024

--

List and Set, both are Interfaces extending Collection interface in Java.

Some of the most used implementations of List in Java are ArrayList, LinkedList.

Some of the most used implementations of Set in Java are HashSet, LinkedHashSet, TreeSet.

List has below features:
* supports dynamic array.
* elements traversal using index.
* allows duplicates value.
* allows null value.

Set has below features:
* supports dynamic array.
* elements traversal with index not possible.
* duplicate values are not allowed. If duplicate entry added, will be ignored.
* HashSet and LinkedHashSet allow only one null value.
* TreeSet do not allow null values.

______________________________

Now coming to Selenium question, why driver.findElements() made to return List and not Set.

Answer: Because, only List allows duplicate values and not Set.

There is a possibility that when we driver.findElements() there could be duplicate elements returned and as well sometimes no elements returned (null).

So the List is the best possible return type for findElements() method.

______________________________

Similarly, why driver.getWindowHandles(); is made to return Set.

Because, every window will have unique window handle and cannot be duplicate and cannot be null.

Swaroop Nadella
Test Automation Engineer, Tech Educator

Subscribe here, to Get an email whenever I publish article on Medium

Core Java and Coding for Automation Testers — Udemy Course

Swaroop Nadella Academy Store — Quizzes and Courses

--

--