About this lesson
Handle a multi-select dropdown in Selenium Java: select, read and deselect options
Selenium Java AssignmentsIntermediateDropdowns
How it works
A select element with the multiple attribute lets the user choose several options. isMultiple() tells you whether a dropdown is that kind. On the Dropdowns page multiSelect returns true, while singleSelect returns false.
On a multi-select, each selectBy... call adds to the selection instead of replacing it, and selecting an option that is already selected changes nothing. getAllSelectedOptions() returns the chosen options in page order, not in the order you picked them.
Deselecting only works on multi-selects. deselectByVisibleText() and its value and index variants remove one option, deselectAll() clears everything, and calling any of them on a single dropdown throws UnsupportedOperationException, for example "You may only deselect options of a multi-select".
About the assignment
The starter wraps multiSelect, a list of five test frameworks. You confirm it allows several choices, select two frameworks, print the selection, take one away and print what is left.
What you will do
- Print
isMultiple()with the labelIs multiple:. - Select both frameworks named in the task with
selectByVisibleText(). - Read
getAllSelectedOptions(), join the option texts with a comma and a space, and print them after the labelSelected:. - Deselect the framework the task names, then print the remaining selection with the label
After deselect:.
Common mistakes
Using getFirstSelectedOption() for the list
It returns only one option. On a multi-select with nothing chosen it throws
NoSuchElementException: No options are selected.Printing the List directly
Printing a
List<WebElement>gives element descriptions, not option names. Map each option to its text first, for example withString.join()on a list of texts.Deselecting something that is not selected
It does nothing and throws nothing. Read the selection back after deselecting instead of assuming the call changed it.
In a real test suite
Multi-selects appear in permission editors, tag pickers and report filters. Many modern apps replace them with custom chip widgets, so check the tag first: Select only works when the element really is a select.
Lesson 29 listed every option. Lesson 31 reads the option a dropdown loads with, using getFirstSelectedOption() on a single dropdown.
Related lessons
- Get All Dropdown Options with getOptions()List every option, selected or not.
- Keyboard - Actions Class Key EventsHold a modifier key with
keyDown(), the same technique as Ctrl-clicking several options. - Select Dropdown Option by Visible TextThe
selectByVisibleText()basics on a single dropdown.
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/Dropdowns");
Select multiSelect = new Select(driver.findElement(By.id("multiSelect")));
// TODO: Check if it's a multi-select
// TODO: Select "Selenium" and "Cypress"
// TODO: Print all selected options
// TODO: Deselect "Cypress" and print remaining
driver.quit();
}
}