Selecting from Dropdowns with Selenium for Java
Selecting from Dropdowns with Selenium for Java
Learn how to select from dropdowns with Selenium for Java
Support for dropdown web elements is not available in the core Selenium library; you’ll need to include the Selenium Support dependency as well.
Once you’ve added this dependency to your project, you’ll be able to use the Select class, which represents dropdown elements in Selenium.
How to Select from Dropdowns with Selenium
The Select() instructor takes a web element, so we’ll need to go and grab the ID of the dropdown.
We can see here, the ID is “spices-select-single”. So, let’s add that to our project. Again, this constructor takes a web element, so we need to first do driver.findElement(By.id()) and then pass it the locator.
Next, let’s identify an option that we’d like to select. We’ll need to get that value from the website.
Looking at the DOM, we see the 4 options that are available in this dropdown. Each of them have a value, and then they also have the visible text. Let’s use the value for this case. I’ll go ahead and choose “ginger”. We’ll paste our option here.
Next, we use our dropdown object to select. Notice there are 3 ways to select an option from the dropdown. You can selectByValue, which we have here. We can selectByIndex, which means give it the number of the order in which the option appears in the dropdown. Or you can selectByVisibleText.
We’ll choose to selectByValue() and pass in our option. Great. That’s all we need to select an option from a dropdown.
But what about reading the selected options for verification purposes?
We can create a list and we’ll call this “selectedOptions” and we’ll use our dropdown object. Notice we can getAllSelectedOptions, we can get all the options whether they’re selected or not with getOptions, or we can get the getFirstSelectedOption.
Let’s getAllSelectedOptions(). As you can see, this returns a list of web elements.
Now, let’s make sure that there’s only 1 selected option returned in this list — assertEquals(1, selectedOptions.size()).
Next, we’ll verify that that selectedOptions is indeed “ginger”. Awesome.
This is all the code that we need to select a single option from a dropdown and to verify that that option is indeed selected.
How to Select Multiple Dropdown Options with Selenium
How about selecting multiple options? Let’s do that in this test method.
We’ll create our Select dropdown and let’s get the ID from the website and that’s “spices-select-multi”. We’ll add it to our code base.
Let’s define the options that we like to select. We’ll call this “options” and we’ll say, List.of(). And let’s go get the values from the website. So, we see each of the options here, so we can select these values. This time let’s choose “ginger” and “chili-powder”.
We’ll use the dropdown object to selectByValue each of the values that are in our list. Excellent, this is all we need to select multiple options from the dropdown menu.
Now, let’s read the selectedOptions from our dropdown. And again, that’s dropdown.getAllSelectedOptions().
Next, let’s make sure the number of options that are selected were actually the same number that we chose to select.
Last but not least, let’s make sure that each of the values that are selected match the values that we specified in options. So, we’ll take the list of web elements that was returned. We’ll turn this into a stream. We’ll get the value attribute for each of them.
Let’s turn this into a list using toList() and then let’s make sure that this list containsAll the options that we selected. Awesome.
There you have it; this is the code to select multiple options, and this is the code to read the options that are selected.
Resources
- Git Repo – DropdownTests.java
- Applitools Kitchen – Dropdown Select Testing
- MVN Repository – Selenium Support Dependency
Schedule a free Applitools Demonstration
Request DemoHere’s the code sample from our tutorial on how to select from dropdowns with Selenium for Java