Skip to content
CodeKruPro

Handle Multi-Select Dropdown

30/70

Handle Multi-Select Dropdown

Multi-select dropdowns allow selecting multiple options. Use isMultiple() to check, and getAllSelectedOptions() to get all selections.

The multiSelect dropdown has: Selenium, Playwright, Cypress, TestCafe, WebDriverIO.

HTML Structure:

<select id="multiSelect" multiple>
  <option value="selenium">Selenium</option>
  <option value="playwright">Playwright</option>
  <option value="cypress">Cypress</option>
  <option value="testcafe">TestCafe</option>
  <option value="webdriverio">WebDriverIO</option>
</select>

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Verify the multiSelect dropdown supports multiple selections
  3. Select "Selenium" and "Cypress"
  4. Print all selected options
  5. Deselect "Cypress"
  6. Print remaining selected options

Expected output:

Is multiple: true
Selected: Selenium, Cypress
After deselect: Selenium

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

  1. Print isMultiple() with the label Is multiple:.
  2. Select both frameworks named in the task with selectByVisibleText().
  3. Read getAllSelectedOptions(), join the option texts with a comma and a space, and print them after the label Selected:.
  4. 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 with String.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.

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.

Main.java
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();
    }
}