3

When we get options of a dropdown in Selenium Webdriver the following way

 Select dropdown = new Select(driver.findElement(By.id(dropDownId)));
 List<WebElement> options = dropdown.getOptions();

What is order of elements in the list returned by getOptions( ) method?
I want to have the elements in the order they appear in the Dropdown. Does it happens that way?

4 Answers 4

3

Yes, It returns in the order only

Implimentation of getOptions() method in Select class

 /**
   * @return All options belonging to this select tag
   */
  public List<WebElement> getOptions() {
    return element.findElements(By.tagName("option"));
  }
0
0

Basically getoptions() returns All options belonging to your select tag. It finds value in sequence.

For example if we have array in java then when array display value from index 0 to end of array. In same way getoptions() starts to get value from 1st select and goes till last value.

0

List is defined as part of the Java framework

A synopsis is, you can put items into the List in any order you wish, and that is the order in which that are returned unless you specify otherwise.

-1

I haven't used the getOptions() function before, but with every other find or get function I've used in Selenium, the objects are returned in the order in which they appear in the page source.

That said, why don't you just test it by tweaking your code to display the text of the elements, and see if they are in the same order you see in the browser?

2
  • 1
    I tried it, it displays in the same order but i couldn't decide as it could be random as well as long as i don't know the reason. Commented Dec 16, 2014 at 19:28
  • It's not random. The reason is that the elements are displayed in the order in which they appear in the page source. Commented Dec 16, 2014 at 19:53

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.