Selenium Form Elements - Select Class, Dropdowns, Checkboxes & Text Boxes

Forms are where most test flows start: dropdowns, checkboxes, radio buttons, text boxes and text areas. This guide covers each with Selenium 4 code — including the Select class in full, multi-select and custom (non-<select>) dropdowns, and the mistakes that make form tests flaky. Practise on DemoQA's Select Menu and The Internet's Checkboxes page.

Advertisement

HTML dropdowns come in two kinds, and they're automated differently:

  • Native dropdowns built with <select> and <option> — use Selenium's Select class.
  • Custom dropdowns built from <div>, <ul>/<li> or <input> (common in React and Angular apps) — click to open, then click the option. The Select class won't work on them.
import org.openqa.selenium.support.ui.Select;

Select colour = new Select(driver.findElement(By.id("oldSelectMenu")));

Passing any element other than a <select> throws UnexpectedTagNameException — the quickest way to tell which kind of dropdown you have.

Selecting an option

Method Selects by Example Reliability
selectByVisibleText() The text the user sees selectByVisibleText("Green") ⭐ Best for readable tests
selectByValue() The option's value attribute selectByValue("2") Stable when values are fixed codes
selectByIndex() Position, starting at 0 selectByIndex(3) Breaks when options are added or reordered
colour.selectByVisibleText("Green");
colour.selectByValue("2");
colour.selectByIndex(3);                        // the 4th option

Selecting a disabled option throws an exception in Selenium 4 — which is useful when you're testing that an option really can't be chosen.

Reading and verifying a dropdown

String selected = colour.getFirstSelectedOption().getText();         // what's selected now
List<WebElement> options = colour.getOptions();                      // every option
List<String> texts = options.stream().map(WebElement::getText).toList();

Assert.assertEquals(selected, "Green");
Assert.assertTrue(texts.contains("Purple"));

// Verify the options are sorted (a common test)
List<String> sorted = new ArrayList<>(texts);
sorted.sort(String.CASE_INSENSITIVE_ORDER);
Assert.assertEquals(texts, sorted, "options are not in alphabetical order");

Multi-select dropdowns

A <select multiple> allows several options at once. Check with isMultiple():

Select cars = new Select(driver.findElement(By.id("cars")));

if (cars.isMultiple()) {
    cars.selectByValue("volvo");                  // value attribute (lowercase here)
    cars.selectByVisibleText("Audi");             // visible text
    List<WebElement> chosen = cars.getAllSelectedOptions();
    Assert.assertEquals(chosen.size(), 2);

    cars.deselectByVisibleText("Audi");
    cars.deselectAll();                           // multi-select only
}

Note the difference between a value ("volvo") and visible text ("Volvo") — they're often not identical, and mixing them up is a common reason NoSuchElementException: Cannot locate option appears. Deselect methods throw UnsupportedOperationException on a single-select dropdown.

Custom dropdowns

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.findElement(By.id("react-select-city")).click();                 // open
wait.until(ExpectedConditions.elementToBeClickable(
        By.xpath("//div[@role='option'][normalize-space()='Pune']"))).click();   // pick

// Searchable ("autocomplete") dropdowns: type, wait for the option, then select it
WebElement input = driver.findElement(By.cssSelector("#city input"));
input.sendKeys("Pun");
wait.until(ExpectedConditions.visibilityOfElementLocated(
        By.xpath("//div[@role='option'][normalize-space()='Pune']"))).click();

Options in custom dropdowns often render only after opening (or after an API call), so always wait for the option before clicking. More practice: Dropdown Practice Exercises.

Checkboxes and Radio Buttons

Checking state

WebElement terms = driver.findElement(By.id("terms"));
terms.isSelected();     // checked?
terms.isEnabled();      // can it be changed?
terms.isDisplayed();    // visible?

These work only on elements that exist — isDisplayed() on a missing element throws NoSuchElementException. To check that something is absent, use findElements(...).isEmpty() rather than a try/catch.

Select only if needed ⭐

public void setChecked(By locator, boolean shouldBeChecked) {
    WebElement box = driver.findElement(locator);
    if (box.isSelected() != shouldBeChecked) box.click();
}

Clicking blindly toggles: an already-checked box becomes unchecked. This helper sets the state you want regardless of where it started.

Selecting all checkboxes

List<WebElement> boxes = driver.findElements(By.cssSelector("input[type='checkbox']"));
for (WebElement box : boxes) {
    if (!box.isSelected()) box.click();
}
long checked = boxes.stream().filter(WebElement::isSelected).count();
Assert.assertEquals(checked, boxes.size());

Identifying a specific checkbox

Approach Locator Notes
Unique ID or name By.id("hobbies-reading") Best when stable
Value attribute input[type='checkbox'][value='reading'] Great for groups
Label text //label[normalize-space()='Reading'] Clicking a label toggles its checkbox
Index in a list findElements(...).get(2) Last resort — breaks when order changes

Radio buttons

driver.findElement(By.cssSelector("input[type='radio'][name='gender'][value='female']")).click();
Assert.assertTrue(driver.findElement(By.cssSelector("input[name='gender'][value='female']")).isSelected());

Radios in the same group share a name; selecting one deselects the others, and a radio can't be unchecked by clicking it again.

Custom checkboxes

isSelected() only works on real <input> checkboxes, radios and options. Styled checkboxes built from <div> or <span> report their state through attributes — read aria-checked instead:

WebElement toggle = driver.findElement(By.cssSelector("[role='checkbox'][aria-label='Email alerts']"));
boolean on = "true".equals(toggle.getDomAttribute("aria-checked"));

Some libraries hide the real input and show a styled element over it; clicking the hidden input then fails with "element not interactable" — click the visible label instead.

Text Boxes and Text Areas

WebElement name = driver.findElement(By.id("firstName"));
name.clear();                                           // empty the field first
name.sendKeys("Asha");
String typed = name.getDomProperty("value");            // ⭐ what's in the field now
String hint  = name.getDomAttribute("placeholder");     // attribute from the HTML

WebElement search = driver.findElement(By.name("q"));
search.sendKeys("Selenium WebDriver" + Keys.ENTER);     // type and press Enter
name.sendKeys(Keys.TAB);                                // move focus (triggers blur validation)

// Select all and replace when clear() doesn't trigger the app's change events
name.sendKeys(Keys.chord(Keys.CONTROL, "a"), "New value");   // Keys.COMMAND on macOS

WebElement notes = driver.findElement(By.id("notes"));
notes.sendKeys("First line", Keys.ENTER, "Second line");     // multi-line text area
  • getText() returns an empty string for inputs — the value isn't text content. Use getDomProperty("value") (or getAttribute("value"), deprecated since Selenium 4.27).
  • clear() sometimes doesn't fire the app's events in React forms, so validation doesn't update — the select-all-and-type approach does.
  • Disabled and read-only: isEnabled() is false for disabled fields; read-only fields are enabled but have the readonly attribute.

See how each of these commands travels from your test to the browser in the Selenium WebDriver Visualizer. Over 130 practice exercises: Text Boxes, Radio Buttons & Checkboxes Practice.

From Real Projects

In my automation roles I created, modified and enhanced Selenium WebDriver scripts in Java, organised with POM classes and a business library, and ran them with TestNG on Chrome and Firefox. Exercises like these build exactly the muscle memory you need when a real application — for me, Canolog's sales, inventory and service screens — throws a new kind of element at you. Verify the state after the action — that the option is selected or the box is ticked.

FAQs

How do you handle dropdowns in Selenium?

For <select> dropdowns, wrap the element in the Select class (org.openqa.selenium.support.ui.Select) and use its methods. For custom dropdowns, click to open, wait for the option and click it.

What are the three ways to select a dropdown value?

selectByVisibleText(), selectByValue() and selectByIndex().

How do you get the selected option?

getFirstSelectedOption().getText(), or getAllSelectedOptions() for a multi-select.

How do you check whether a dropdown allows multiple selection?

isMultiple() — true when the <select> has the multiple attribute.

Why does the Select class throw UnexpectedTagNameException?

The element isn't a <select> — it's a custom dropdown, which needs clicks instead.

How do you verify a checkbox or radio button is selected?

isSelected() for real inputs; the aria-checked attribute for custom controls.

How do you read what's typed in a text box?

getDomProperty("value") — getText() returns an empty string for inputs.

How do you type on a new line in a text area?

sendKeys("First line", Keys.ENTER, "Second line").