Welcome back
In the previous article, you learned to ask whether an element is visible, enabled, or selected. Those checks now become part of a real task: filling a form.
A form collects information and choices through controls such as inputs, checkboxes, radio buttons, and dropdowns. Selenium handles each control according to the way it works in HTML.
You already know the everyday actions click(), sendKeys(), and clear(). This lesson connects those actions with isSelected() and introduces Selenium’s Select helper for real HTML select lists.
The examples use four TestKru practice pages:
How a form is built
Form controls can look similar on screen but follow different rules. A checkbox and a radio button are both clicked, for example, but they do not make choices in the same way.
| Control | Usual user action | Selenium approach | State or result to read |
|---|---|---|---|
| Text input | Type text | clear(), then sendKeys() | getAttribute("value") |
| Checkbox | Turn a choice on or off | click() | isSelected() |
| Radio button | Choose one item in a group | click() | isSelected() |
| HTML select list | Choose one or more options | Select methods | Selected option text or value |
The important habit stays the same for every row:
1. Find the control.
2. Check its starting state when that state matters.
3. Perform the user action.
4. Read the result.
5. Assert the result with TestNG.
Do not stop after an action. A form test should prove which value or choice the page now holds.
Type into a text field
Open TestKru Text Fields. Use the last-name field with the unique id lastNameWithPlaceholder. Its placeholder is Enter your last name....
A placeholder is a hint shown while a field is empty. It is not the value entered by the user. Clear the field, type codekru, then read its current value:
WebElement lastName =
driver.findElement(By.id("lastNameWithPlaceholder"));
lastName.clear();
lastName.sendKeys("codekru");
Assert.assertEquals(
lastName.getAttribute("value"),
"codekru");
sendKeys() enters keyboard text. clear() removes existing editable text first. getAttribute("value") reads what is currently inside the input.
Do not use getText() for this check. An input stores typed characters in its value, not as inner text. Also remember that sendKeys() adds to existing content. The page has a pre-filled field, preFilledTextField, that starts with Codekru. Typing without clearing it would append the new text.
Skip the page’s disabled, read-only, hidden, and duplicate-id fields. They do not match this normal editing path.
Check a checkbox
A checkbox represents a choice that can be on or off. A person can leave several checkboxes selected, or none. On TestKru Checkboxes, firstSelect1 starts unselected. One click selects it, and another click clears it.
WebElement checkbox = driver.findElement(By.id("firstSelect1"));
Assert.assertFalse(checkbox.isSelected());
checkbox.click();
Assert.assertTrue(checkbox.isSelected());
checkbox.click();
Assert.assertFalse(checkbox.isSelected());
That two-way behavior is called a toggle. The same checkbox changes between selected and unselected whenever it is clicked.
Use isSelected() to read the current state. Do not use getAttribute("checked") as a replacement. Your test cares whether the choice is currently selected, and isSelected() asks that question directly.
Page rules can also affect checkboxes. In the Single-select Checkboxes row, choosing a different box clears the previous choice. Clicking the same box twice still turns it off. This is TestKru page behavior, not a rule for every checkbox.
The multi-select row behaves like a common checkbox group. Both firstSelect2 and secondSelect2 can stay selected:
WebElement firstChoice = driver.findElement(By.id("firstSelect2"));
WebElement secondChoice = driver.findElement(By.id("secondSelect2"));
firstChoice.click();
secondChoice.click();
Assert.assertTrue(firstChoice.isSelected());
Assert.assertTrue(secondChoice.isSelected());
Do not click displayed but disabled firstSelect3 or hidden secondSelect4.
Pre-selected firstSelect5 needs care. An unconditional click clears it. If the goal is “ensure selected,” use the current state:
WebElement preSelected = driver.findElement(By.id("firstSelect5"));
if (!preSelected.isSelected()) {
preSelected.click();
}
Assert.assertTrue(preSelected.isSelected());
Use a direct click when the toggle itself is the behavior under test.
Open TestKru Radio Buttons before you use the next ids. This page reuses names such as firstSelect1, but these are radio buttons, not the checkboxes from the previous section.
A radio button usually represents one choice from a group, such as a single preferred language. The user should not leave two answers selected in that group. Here, firstSelect1, secondSelect1, and thirdSelect1 share the HTML name select. That shared name places them in one group.
Click the first radio, then choose the second:
WebElement firstRadio = driver.findElement(By.id("firstSelect1"));
WebElement secondRadio = driver.findElement(By.id("secondSelect1"));
firstRadio.click();
Assert.assertTrue(firstRadio.isSelected());
secondRadio.click();
Assert.assertFalse(firstRadio.isSelected());
Assert.assertTrue(secondRadio.isSelected());
The second click moves the group’s choice. Clicking the selected radio again does not turn it off. Choose another radio in the group to change the answer.
Grouping comes from the HTML name, not the round shape. In the page’s Multi-select Radio Buttons, firstSelect2 and secondSelect2 have different names, so both can be selected.
Do not click disabled firstSelect3 or hidden radios. In the pre-selected row, firstSelect5 starts selected, and clicking secondSelect5 moves the choice.
Select from a dropdown
Open TestKru Dropdowns. The first control is a drop-down box. In HTML, that control is a <select> element, and each choice inside it is an <option> element.
Selenium provides a helper class named Select for this structure. You will import org.openqa.selenium.support.ui.Select in the complete class.
Calling click() on the drop-down box does not tell Selenium which option to choose. Wrap the found <select> in Select, then ask that helper to pick an option:
Select language = new Select(
driver.findElement(By.id("singleSelect")));
Select works only with a real HTML <select>. If you wrap a div, a button, or another control, Selenium throws UnexpectedTagNameException. In beginner terms, the helper looked for a select list and received a different kind of element.
Some pages draw a custom menu with div or li elements so it looks like a dropdown. Select will not work on those. They come later in the course.
The methods in this section follow Selenium’s official select list guidance. Everyday click, type, and clear behavior is covered in the element interaction guide.
Select by the wording you see
The singleSelect list starts on the disabled placeholder Select Language. Choose Java by the wording shown to the user:
Select language = new Select(
driver.findElement(By.id("singleSelect")));
language.selectByVisibleText("Java");
selectByVisibleText("Java") matches the wording the user sees. On this page, it also updates singleSelectResult to Selected: Java.
Missing option text causes NoSuchElementException. Check spelling, spaces, capitals, and the actual options.
Select by value or index
An option can also have an HTML value. Java’s visible text is Java, and its value is java:
language.selectByValue("java");
Use value when it is stable. It is not necessarily the visible text.
You can also choose by position:
language.selectByIndex(1);
Indexes start at zero. On this page, index 0 is the disabled Select Language placeholder, so Java is index 1.
Index selection is less readable and can break when options move. Prefer visible text unless the task requires value or index.
These three methods choose the same Java option on this page:
| Method | What you pass | When to use it |
|---|---|---|
selectByVisibleText("Java") | The wording the user sees | First choice for beginners |
selectByValue("java") | The option’s HTML value | When that value is stable |
selectByIndex(1) | The option’s position, starting at 0 | Only when you must use position |
Do not mix them up. Java is visible text. java is the value. 1 is the index of Java after the placeholder.
Read the selected option
Do not assume that a selection method worked. Read the selected option and assert it:
WebElement selected = language.getFirstSelectedOption();
Assert.assertEquals(selected.getText(), "Java");
Assert.assertEquals(selected.getAttribute("value"), "java");
getFirstSelectedOption() returns the first selected option. Read its wording with getText() or its value with getAttribute("value").
You can inspect every available option with getOptions():
Assert.assertEquals(language.getOptions().size(), 6);
The count is six because it includes the disabled placeholder plus Java, Python, JavaScript, C#, and Ruby.
getFirstSelectedOption() throws when nothing is selected. TestKru’s multi-select starts empty, so select first or check the list from getAllSelectedOptions().
preSelectedDropdown starts with JavaScript selected. Read its first selected option to prove that state.
Work with more than one option
The element multiSelect has the HTML multiple setting and shows five testing tools. Confirm that Selenium recognizes it as a multi-select:
Select tools = new Select(
driver.findElement(By.id("multiSelect")));
Assert.assertTrue(tools.isMultiple());
Now select two options and read them together. Java’s List holds several items. Here it holds the selected options so you can count them and read each one:
tools.selectByVisibleText("Selenium");
tools.selectByVisibleText("Playwright");
List<WebElement> selectedTools =
tools.getAllSelectedOptions();
Assert.assertEquals(selectedTools.size(), 2);
Assert.assertEquals(selectedTools.get(0).getText(), "Selenium");
Assert.assertEquals(selectedTools.get(1).getText(), "Playwright");
deselectAll() and methods such as deselectByVisibleText(...) are only for multi-select lists. Calling a deselect method on a single-select throws UnsupportedOperationException. Selenium is telling you that clearing options is only allowed when the list supports more than one choice.
disabledDropdown is displayed, but isEnabled() returns false. Selenium can still find that drop-down. It will not let you choose an option from it. Check isEnabled() first and leave the control unchanged.
A complete TestNG example
The complete class now joins all four form controls. It assumes your existing Maven project already contains Selenium 4 and TestNG.
Save the class
Create this file:
src/test/java/com/codekru/tests/FormControlsPracticeTest.java
If your project uses another base package, change the package line to match it.
The complete example
package com.codekru.tests;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class FormControlsPracticeTest {
private WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void shouldFillInputCheckboxRadioAndSelect() {
driver.get("https://testkru.com/Elements/TextFields");
Assert.assertEquals(driver.getTitle(), "Text Fields");
WebElement lastName =
driver.findElement(By.id("lastNameWithPlaceholder"));
lastName.clear();
lastName.sendKeys("codekru");
Assert.assertEquals(
lastName.getAttribute("value"),
"codekru");
driver.get("https://testkru.com/Elements/Checkboxes");
Assert.assertEquals(driver.getTitle(), "Checkboxes");
WebElement singleCheckbox =
driver.findElement(By.id("firstSelect1"));
Assert.assertFalse(singleCheckbox.isSelected());
singleCheckbox.click();
Assert.assertTrue(singleCheckbox.isSelected());
WebElement firstMultiCheckbox =
driver.findElement(By.id("firstSelect2"));
WebElement secondMultiCheckbox =
driver.findElement(By.id("secondSelect2"));
firstMultiCheckbox.click();
secondMultiCheckbox.click();
Assert.assertTrue(firstMultiCheckbox.isSelected());
Assert.assertTrue(secondMultiCheckbox.isSelected());
driver.get("https://testkru.com/Elements/RadioButtons");
Assert.assertEquals(driver.getTitle(), "Radio Buttons");
WebElement firstRadio =
driver.findElement(By.id("firstSelect1"));
WebElement secondRadio =
driver.findElement(By.id("secondSelect1"));
firstRadio.click();
Assert.assertTrue(firstRadio.isSelected());
secondRadio.click();
Assert.assertFalse(firstRadio.isSelected());
Assert.assertTrue(secondRadio.isSelected());
driver.get("https://testkru.com/Elements/Dropdowns");
Assert.assertEquals(driver.getTitle(), "Dropdowns");
Select language = new Select(
driver.findElement(By.id("singleSelect")));
language.selectByVisibleText("Java");
Assert.assertEquals(
language.getFirstSelectedOption().getText(),
"Java");
Assert.assertEquals(
driver.findElement(By.id("singleSelectResult")).getText(),
"Selected: Java");
Select tools = new Select(
driver.findElement(By.id("multiSelect")));
Assert.assertTrue(tools.isMultiple());
tools.selectByVisibleText("Selenium");
tools.selectByVisibleText("Playwright");
List<WebElement> selectedTools =
tools.getAllSelectedOptions();
Assert.assertEquals(selectedTools.size(), 2);
Assert.assertEquals(
selectedTools.get(0).getText(),
"Selenium");
Assert.assertEquals(
selectedTools.get(1).getText(),
"Playwright");
WebElement disabledDropdown =
driver.findElement(By.id("disabledDropdown"));
Assert.assertTrue(disabledDropdown.isDisplayed());
Assert.assertFalse(disabledDropdown.isEnabled());
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Understand each part
The imports add Selenium, Select, TestNG, and Java’s List type. setUp() starts Chrome and maximizes the window. The test checks each page title before interacting.
On Text Fields, it clears the unique last-name input, types codekru, and reads the value back. On Checkboxes, it proves that firstSelect1 changes from unselected to selected. It then proves that both controls in the multi-select checkbox row can remain selected.
On Radio Buttons, the first choice becomes selected. Selecting the second choice clears the first because both share the same group name.
On Dropdowns, the test chooses Java and checks both the selected option and Selected: Java. It then proves that multiSelect accepts Selenium and Playwright together.
The test checks the disabled dropdown without wrapping or selecting it. tearDown() calls quit(), and its null check protects cleanup if Chrome could not start.
Run it and check the result
- Save the class in the path shown above.
- Open
FormControlsPracticeTest.javain IntelliJ. - Run
shouldFillInputCheckboxRadioAndSelectas a TestNG test. - You can also use the TestNG Maven command already configured in your project.
Chrome should type codekru, select three checkboxes across the demonstrated rows, move a radio choice from first to second, choose Java, choose two tools, inspect the disabled dropdown, pass every assertion, and close.
If the test fails, identify the page and read the assertion’s actual and expected values. Check starting state, exact text, and the selected options.
Common beginner mistakes
- Reading typed input with
getText(). UsegetAttribute("value")for text inside an input. - Typing without clearing existing text.
sendKeys()appends. Useclear()first when the form should contain only the new value. - Clicking a checkbox without checking the starting state. A pre-selected checkbox becomes unselected after a click.
- Treating every checkbox group as multi-select. A page can add logic that clears another checkbox. Assert the actual behavior.
- Expecting a selected radio to toggle off. A normal radio choice stays selected until another radio in the same name group is chosen.
- Assuming round controls always share a group. Radio grouping comes from the HTML
name, not the shape. - Using
getAttribute("checked")instead ofisSelected(). Use the state method for checkboxes, radios, and option elements. - Using
Selecton a custom dropdown. The helper works only with a real HTML<select>and its<option>elements. - Choosing the placeholder by index. On TestKru, index
0is the disabledSelect Languageoption. Java is index1. - Deselecting from a single-select. Deselect methods are only for a select list that allows multiple options.
- Selecting from a disabled dropdown. Check
isEnabled()first and leave the control unchanged when it is disabled. - Calling
getFirstSelectedOption()on an empty multi-select. Select something first, or readgetAllSelectedOptions()and check whether the list is empty.
Practice assignment
Practice one form skill at a time. Start with the dropdown wording that a user can see:
Continue practicing: Select Dropdown Option by Visible Text
Do not look for a completed solution. Inspect the target control, perform the requested action, and assert the selected result.
For extra practice, continue with Handle Checkboxes – Click and Verify, Handle Radio Buttons, Select Dropdown by Value, and Handle Multi-Select Dropdown.
You can browse every exercise in the Selenium learning catalog.
What is next
You can now fill the standard controls found in many forms. You can type and read an input, prove a checkbox state, move a radio choice, and use Select with single-select and multi-select lists.
The next lesson is Dynamic Components: Auto-suggestions, Tables, Pagination, and Hidden Elements. Those controls change while the page is running, contain repeated data, or need more than one simple action. You will build on today’s find, act, read, and assert pattern while handling that extra movement.
