We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Handle multi-select lists with selection, deselection, and verification methods
Multi-select dropdowns use the multiple attribute and allow selecting and deselecting several options simultaneously.
Same as single-select:
These methods add to existing selections (don't clear previous selections)
Only available for multi-select:
List<WebElement> of all currently selected <option> elements. Loop through to verify selections.true if the select has the multiple attribute. Always check before using deselect methods.isMultiple() to confirm it's actually multi-select before using deselect methodsdeselectBy... methods only on multi-selects to avoid confusiongetAllSelectedOptions() to verify multiple selectionsdeselectAll() to start with a clean slateImagine a page with a Breakfast Selector (multi-select):
Selected: Eggs, Sausages
Options Details (Pre-selected: Eggs, Sausages):
Hold Ctrl/Cmd and click to select multiple items!
Your Selenium code will automate these steps using the Select class's selection and deselection methods.
12345678910111213141516171819202122232425262728293031WebElement breakfastSelectElement = driver.findElement(By.id("multi"));
Select breakfastSelect = new Select(breakfastSelectElement);
// Verify it's a multi-select
if (breakfastSelect.isMultiple()) {
System.out.println("This is a multi-select list");
}
// Multi-select: select "Ham" by value and "Onion gravy" by index
breakfastSelect.selectByValue("ham");
breakfastSelect.selectByIndex(3); // "Onion gravy"
// Multi-select: deselect "Eggs" by value
breakfastSelect.deselectByValue("eggs");
// Inspect selected breakfast options
List<WebElement> selectedBreakfastOptions = breakfastSelect.getAllSelectedOptions();
System.out.println("Total selected: " + selectedBreakfastOptions.size());
for (WebElement option : selectedBreakfastOptions) {
System.out.println("Selected: " + option.getText());
}
// Output:
// Selected: Ham
// Selected: Sausages
// Selected: Onion gravy
// Clear all selections
breakfastSelect.deselectAll();
driver.quit();<select> element using its id="multi".<select> in a Select object, ready for multi-select operations.true if the select has the multiple attribute. Always check this before using deselect methods to avoid exceptions.value="ham". This adds to the existing selections (doesn't clear previous selections).value="eggs". This only works because this is a multi-select list. Attempting this on a single-select will throw UnsupportedOperationException.List of all currently selected <option> elements. Use this to verify multiple selections.Always check select.isMultiple() before using deselect... methods to avoid UnsupportedOperationException.
When debugging flaky multi-select tests, print out getAllSelectedOptions() to confirm exactly what's selected at each step in your scenario.
Use deselectAll() at the start of your test to ensure a clean state, especially if the page has pre-selected options.