Skip to main content

How to get all the options in a select tag using Selenium Webdriver

How to get all the options in a select tag using Selenium Webdriver

Key takeaways

  • The Select class exposes getOptions() for real select elements.
  • Custom dropdowns built from divs need normal element queries instead.
  • Assert on the option text or value, not the count alone, which passes on the wrong list.

During UI test automation you will need to write tests that validate the options that are present in a select tag.
Getting all the options in the select tag is very simple using Selenium Webdriver and below is how it is done.

1. In the page object model of the page you create a WebElement that identifies the select tag; example class presented below:


import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class PageObject {

public String[] occupantNoOptionExpectedArray = {"1", "2", "3", "4", "5+"};

@FindBy(xpath = "//select[@id='numberofoccupants']")
public WebElement noOfOccupantsDropdown;

}

2. In the test you will create a Select object (class org.openqa.selenium.support.ui.Select) like in the below example:


import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class OccupantPageTest {

PageObject pageObject = new PageObject();
[emaillocker]
@Test
public void test_Occupants_Drop_Down_Values() throws Exception{
Select selectBedroom = new Select(pageObject.noOfOccupantsDropdown);
List<WebElement> optionSelect = selectBedroom.getOptions();

for (int i = 0; i < optionSelect.size(); i++){
Assert.assertEquals(optionSelect.get(i).getText(), pageObject.occupantNoOptionExpectedArray[i], "Values in the drop down for number of occupants are wrong"  );
}
}
}

That’s it. Simple. Enjoy.
[/emaillocker]

Frequently Asked Questions

How do I read every option from a dropdown?

Wrap the element in Selenium’s Select class and call getOptions(), which returns the option elements as a list. Read getText() from each one to compare against your expected values.

Does this work for custom dropdowns?

No. The Select class only works on a real HTML select element. A dropdown built from divs and list items is just ordinary markup, so you locate and read those elements the normal way.

What should the assertion check?

The text or the values, not only the count. A test that asserts five options pass happily when the list is five wrong options, which is exactly the regression you were trying to catch.

Chris

Head of Content at LoadFocus

Chris leads content and oversees development across the services this blog covers. The guides and tool comparisons here come out of the same decisions that shape what ships.

Weekly tips right in your inbox.

Don’t miss out on the latest testing tips and tricks, tools and news related to software testing.

Already +20,000 members in the list.

The Load Testing Platform with AI That Explains Your Results

Load test websites and APIs from 25 cloud regions, monitor page speed and uptime, and get AI analysis that explains your results in plain English.

Cloud load testing, website speed and API monitoring in one place.

Hi there! Can I share a cool thing I'm working on with you?

A software testing newsletter that sparks joy.

Our goal is to create helpful content for software developers and testers.

If you're not into it, you can unsubscribe instantly.