Skip to main content
SeleniumDecoded

Forms and Inputs

Master form interactions including text fields, checkboxes, radio buttons, and form submission.

Selenium 3 & 4 Stable

Forms are the backbone of web interactions. This guide covers all common form elements and how to interact with them reliably.

Text Input Fields

Working with Text Inputs
Selenium 3 & 4 Stable
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Keys;
// Basic text entry
WebElement usernameField = driver.findElement(By.id("username"));
usernameField.sendKeys("testuser");
// Clear existing text first
WebElement emailField = driver.findElement(By.id("email"));
emailField.clear();
emailField.sendKeys("test@example.com");
// Get current value
String currentValue = emailField.getAttribute("value");
System.out.println("Current value: " + currentValue);
// Check if field is enabled
boolean isEnabled = emailField.isEnabled();
// Check if field is displayed
boolean isVisible = emailField.isDisplayed();
// Password fields work the same way
WebElement passwordField = driver.findElement(By.id("password"));
passwordField.sendKeys("secretPassword123");
// Textarea (multi-line text)
WebElement bioField = driver.findElement(By.id("bio"));
bioField.sendKeys("Line 1" + Keys.ENTER + "Line 2");
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Basic text entry
username_field = driver.find_element(By.ID, "username")
username_field.send_keys("testuser")
# Clear existing text first
email_field = driver.find_element(By.ID, "email")
email_field.clear()
email_field.send_keys("test@example.com")
# Get current value
current_value = email_field.get_attribute("value")
print(f"Current value: {current_value}")
# Check if field is enabled
is_enabled = email_field.is_enabled()
# Check if field is displayed
is_visible = email_field.is_displayed()
# Password fields work the same way
password_field = driver.find_element(By.ID, "password")
password_field.send_keys("secretPassword123")
# Textarea (multi-line text)
bio_field = driver.find_element(By.ID, "bio")
bio_field.send_keys("Line 1" + Keys.ENTER + "Line 2")
const { By, Key } = require('selenium-webdriver');
// Basic text entry
const usernameField = await driver.findElement(By.id('username'));
await usernameField.sendKeys('testuser');
// Clear existing text first
const emailField = await driver.findElement(By.id('email'));
await emailField.clear();
await emailField.sendKeys('test@example.com');
// Get current value
const currentValue = await emailField.getAttribute('value');
console.log(`Current value: ${currentValue}`);
// Check if field is enabled
const isEnabled = await emailField.isEnabled();
// Check if field is displayed
const isVisible = await emailField.isDisplayed();
// Password fields work the same way
const passwordField = await driver.findElement(By.id('password'));
await passwordField.sendKeys('secretPassword123');
// Textarea (multi-line text)
const bioField = await driver.findElement(By.id('bio'));
await bioField.sendKeys('Line 1', Key.ENTER, 'Line 2');
using OpenQA.Selenium;
// Basic text entry
IWebElement usernameField = driver.FindElement(By.Id("username"));
usernameField.SendKeys("testuser");
// Clear existing text first
IWebElement emailField = driver.FindElement(By.Id("email"));
emailField.Clear();
emailField.SendKeys("test@example.com");
// Get current value
string currentValue = emailField.GetAttribute("value");
Console.WriteLine($"Current value: {currentValue}");
// Check if field is enabled
bool isEnabled = emailField.Enabled;
// Check if field is displayed
bool isVisible = emailField.Displayed;
// Password fields work the same way
IWebElement passwordField = driver.FindElement(By.Id("password"));
passwordField.SendKeys("secretPassword123");
// Textarea (multi-line text)
IWebElement bioField = driver.FindElement(By.Id("bio"));
bioField.SendKeys("Line 1" + Keys.Enter + "Line 2");

Checkboxes

Working with Checkboxes
Selenium 3 & 4 Stable
// Find checkbox
WebElement agreeCheckbox = driver.findElement(By.id("agree-terms"));
// Check if already selected
boolean isSelected = agreeCheckbox.isSelected();
// Select only if not already selected
if (!agreeCheckbox.isSelected()) {
agreeCheckbox.click();
}
// Uncheck only if currently checked
if (agreeCheckbox.isSelected()) {
agreeCheckbox.click();
}
// Toggle checkbox (switch state)
agreeCheckbox.click();
// Work with multiple checkboxes
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type='checkbox']"));
for (WebElement checkbox : checkboxes) {
if (!checkbox.isSelected()) {
checkbox.click(); // Select all
}
}
// Select checkbox by label text
WebElement label = driver.findElement(By.xpath("//label[text()='Subscribe to newsletter']"));
label.click(); // Clicking label toggles associated checkbox
# Find checkbox
agree_checkbox = driver.find_element(By.ID, "agree-terms")
# Check if already selected
is_selected = agree_checkbox.is_selected()
# Select only if not already selected
if not agree_checkbox.is_selected():
agree_checkbox.click()
# Uncheck only if currently checked
if agree_checkbox.is_selected():
agree_checkbox.click()
# Toggle checkbox (switch state)
agree_checkbox.click()
# Work with multiple checkboxes
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
for checkbox in checkboxes:
if not checkbox.is_selected():
checkbox.click() # Select all
# Select checkbox by label text
label = driver.find_element(By.XPATH, "//label[text()='Subscribe to newsletter']")
label.click() # Clicking label toggles associated checkbox
// Find checkbox
const agreeCheckbox = await driver.findElement(By.id('agree-terms'));
// Check if already selected
const isSelected = await agreeCheckbox.isSelected();
// Select only if not already selected
if (!(await agreeCheckbox.isSelected())) {
await agreeCheckbox.click();
}
// Uncheck only if currently checked
if (await agreeCheckbox.isSelected()) {
await agreeCheckbox.click();
}
// Toggle checkbox (switch state)
await agreeCheckbox.click();
// Work with multiple checkboxes
const checkboxes = await driver.findElements(By.css("input[type='checkbox']"));
for (const checkbox of checkboxes) {
if (!(await checkbox.isSelected())) {
await checkbox.click(); // Select all
}
}
// Select checkbox by label text
const label = await driver.findElement(By.xpath("//label[text()='Subscribe to newsletter']"));
await label.click(); // Clicking label toggles associated checkbox
// Find checkbox
IWebElement agreeCheckbox = driver.FindElement(By.Id("agree-terms"));
// Check if already selected
bool isSelected = agreeCheckbox.Selected;
// Select only if not already selected
if (!agreeCheckbox.Selected)
{
agreeCheckbox.Click();
}
// Uncheck only if currently checked
if (agreeCheckbox.Selected)
{
agreeCheckbox.Click();
}
// Toggle checkbox (switch state)
agreeCheckbox.Click();
// Work with multiple checkboxes
IList<IWebElement> checkboxes = driver.FindElements(By.CssSelector("input[type='checkbox']"));
foreach (IWebElement checkbox in checkboxes)
{
if (!checkbox.Selected)
{
checkbox.Click(); // Select all
}
}
// Select checkbox by label text
IWebElement label = driver.FindElement(By.XPath("//label[text()='Subscribe to newsletter']"));
label.Click(); // Clicking label toggles associated checkbox

Radio Buttons

Working with Radio Buttons
Selenium 3 & 4 Stable
// Select a specific radio button by value
WebElement maleRadio = driver.findElement(
By.cssSelector("input[type='radio'][value='male']")
);
maleRadio.click();
// Check which radio is selected in a group
List<WebElement> genderRadios = driver.findElements(By.name("gender"));
for (WebElement radio : genderRadios) {
if (radio.isSelected()) {
String selectedValue = radio.getAttribute("value");
System.out.println("Selected: " + selectedValue);
break;
}
}
// Select radio by associated label
WebElement label = driver.findElement(By.xpath("//label[text()='Express Shipping']"));
label.click();
// Or find radio by label's 'for' attribute
WebElement radioById = driver.findElement(
By.id(driver.findElement(By.xpath("//label[text()='Standard']")).getAttribute("for"))
);
radioById.click();
# Select a specific radio button by value
male_radio = driver.find_element(
By.CSS_SELECTOR, "input[type='radio'][value='male']"
)
male_radio.click()
# Check which radio is selected in a group
gender_radios = driver.find_elements(By.NAME, "gender")
for radio in gender_radios:
if radio.is_selected():
selected_value = radio.get_attribute("value")
print(f"Selected: {selected_value}")
break
# Select radio by associated label
label = driver.find_element(By.XPATH, "//label[text()='Express Shipping']")
label.click()
# Or find radio by label's 'for' attribute
label_element = driver.find_element(By.XPATH, "//label[text()='Standard']")
radio_by_id = driver.find_element(By.ID, label_element.get_attribute("for"))
radio_by_id.click()
// Select a specific radio button by value
const maleRadio = await driver.findElement(
By.css("input[type='radio'][value='male']")
);
await maleRadio.click();
// Check which radio is selected in a group
const genderRadios = await driver.findElements(By.name('gender'));
for (const radio of genderRadios) {
if (await radio.isSelected()) {
const selectedValue = await radio.getAttribute('value');
console.log(`Selected: ${selectedValue}`);
break;
}
}
// Select radio by associated label
const label = await driver.findElement(By.xpath("//label[text()='Express Shipping']"));
await label.click();
// Or find radio by label's 'for' attribute
const labelElement = await driver.findElement(By.xpath("//label[text()='Standard']"));
const forAttr = await labelElement.getAttribute('for');
const radioById = await driver.findElement(By.id(forAttr));
await radioById.click();
// Select a specific radio button by value
IWebElement maleRadio = driver.FindElement(
By.CssSelector("input[type='radio'][value='male']")
);
maleRadio.Click();
// Check which radio is selected in a group
IList<IWebElement> genderRadios = driver.FindElements(By.Name("gender"));
foreach (IWebElement radio in genderRadios)
{
if (radio.Selected)
{
string selectedValue = radio.GetAttribute("value");
Console.WriteLine($"Selected: {selectedValue}");
break;
}
}
// Select radio by associated label
IWebElement label = driver.FindElement(By.XPath("//label[text()='Express Shipping']"));
label.Click();
// Or find radio by label's 'for' attribute
IWebElement labelElement = driver.FindElement(By.XPath("//label[text()='Standard']"));
IWebElement radioById = driver.FindElement(By.Id(labelElement.GetAttribute("for")));
radioById.Click();

Form Submission

Submitting Forms
Selenium 3 & 4 Stable
// Method 1: Click submit button
WebElement submitButton = driver.findElement(By.cssSelector("button[type='submit']"));
submitButton.click();
// Method 2: Use submit() on form or any form element
WebElement form = driver.findElement(By.id("login-form"));
form.submit();
// Or submit from any element within the form
WebElement usernameField = driver.findElement(By.id("username"));
usernameField.submit(); // Submits the parent form
// Method 3: Press Enter key in a field
WebElement passwordField = driver.findElement(By.id("password"));
passwordField.sendKeys("mypassword" + Keys.ENTER);
// Wait for form submission to complete
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.urlContains("/dashboard"));
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Method 1: Click submit button
submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
submit_button.click()
# Method 2: Use submit() on form or any form element
form = driver.find_element(By.ID, "login-form")
form.submit()
# Or submit from any element within the form
username_field = driver.find_element(By.ID, "username")
username_field.submit() # Submits the parent form
# Method 3: Press Enter key in a field
password_field = driver.find_element(By.ID, "password")
password_field.send_keys("mypassword" + Keys.ENTER)
# Wait for form submission to complete
wait = WebDriverWait(driver, 10)
wait.until(EC.url_contains("/dashboard"))
const { until } = require('selenium-webdriver');
// Method 1: Click submit button
const submitButton = await driver.findElement(By.css("button[type='submit']"));
await submitButton.click();
// Method 2: Use submit() on form or any form element
const form = await driver.findElement(By.id('login-form'));
await form.submit();
// Or submit from any element within the form
const usernameField = await driver.findElement(By.id('username'));
await usernameField.submit(); // Submits the parent form
// Method 3: Press Enter key in a field
const passwordField = await driver.findElement(By.id('password'));
await passwordField.sendKeys('mypassword', Key.ENTER);
// Wait for form submission to complete
await driver.wait(until.urlContains('/dashboard'), 10000);
using OpenQA.Selenium.Support.UI;
// Method 1: Click submit button
IWebElement submitButton = driver.FindElement(By.CssSelector("button[type='submit']"));
submitButton.Click();
// Method 2: Use submit() on form or any form element
IWebElement form = driver.FindElement(By.Id("login-form"));
form.Submit();
// Or submit from any element within the form
IWebElement usernameField = driver.FindElement(By.Id("username"));
usernameField.Submit(); // Submits the parent form
// Method 3: Press Enter key in a field
IWebElement passwordField = driver.FindElement(By.Id("password"));
passwordField.SendKeys("mypassword" + Keys.Enter);
// Wait for form submission to complete
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.Url.Contains("/dashboard"));

Complete Form Example

Complete Registration Form
Selenium 3 & 4 Stable
public void fillRegistrationForm() {
// Text fields
driver.findElement(By.id("firstName")).sendKeys("John");
driver.findElement(By.id("lastName")).sendKeys("Doe");
driver.findElement(By.id("email")).sendKeys("john.doe@example.com");
driver.findElement(By.id("phone")).sendKeys("555-123-4567");
// Password with confirmation
driver.findElement(By.id("password")).sendKeys("SecurePass123!");
driver.findElement(By.id("confirmPassword")).sendKeys("SecurePass123!");
// Radio button - gender
driver.findElement(By.cssSelector("input[value='male']")).click();
// Checkboxes
WebElement newsletter = driver.findElement(By.id("newsletter"));
if (!newsletter.isSelected()) {
newsletter.click();
}
WebElement terms = driver.findElement(By.id("terms"));
if (!terms.isSelected()) {
terms.click();
}
// Textarea
driver.findElement(By.id("bio")).sendKeys(
"Software developer with 5 years of experience."
);
// Submit
driver.findElement(By.id("submit-btn")).click();
}
def fill_registration_form(driver):
# Text fields
driver.find_element(By.ID, "firstName").send_keys("John")
driver.find_element(By.ID, "lastName").send_keys("Doe")
driver.find_element(By.ID, "email").send_keys("john.doe@example.com")
driver.find_element(By.ID, "phone").send_keys("555-123-4567")
# Password with confirmation
driver.find_element(By.ID, "password").send_keys("SecurePass123!")
driver.find_element(By.ID, "confirmPassword").send_keys("SecurePass123!")
# Radio button - gender
driver.find_element(By.CSS_SELECTOR, "input[value='male']").click()
# Checkboxes
newsletter = driver.find_element(By.ID, "newsletter")
if not newsletter.is_selected():
newsletter.click()
terms = driver.find_element(By.ID, "terms")
if not terms.is_selected():
terms.click()
# Textarea
driver.find_element(By.ID, "bio").send_keys(
"Software developer with 5 years of experience."
)
# Submit
driver.find_element(By.ID, "submit-btn").click()
async function fillRegistrationForm(driver) {
// Text fields
await driver.findElement(By.id('firstName')).sendKeys('John');
await driver.findElement(By.id('lastName')).sendKeys('Doe');
await driver.findElement(By.id('email')).sendKeys('john.doe@example.com');
await driver.findElement(By.id('phone')).sendKeys('555-123-4567');
// Password with confirmation
await driver.findElement(By.id('password')).sendKeys('SecurePass123!');
await driver.findElement(By.id('confirmPassword')).sendKeys('SecurePass123!');
// Radio button - gender
await driver.findElement(By.css("input[value='male']")).click();
// Checkboxes
const newsletter = await driver.findElement(By.id('newsletter'));
if (!(await newsletter.isSelected())) {
await newsletter.click();
}
const terms = await driver.findElement(By.id('terms'));
if (!(await terms.isSelected())) {
await terms.click();
}
// Textarea
await driver.findElement(By.id('bio')).sendKeys(
'Software developer with 5 years of experience.'
);
// Submit
await driver.findElement(By.id('submit-btn')).click();
}
public void FillRegistrationForm()
{
// Text fields
driver.FindElement(By.Id("firstName")).SendKeys("John");
driver.FindElement(By.Id("lastName")).SendKeys("Doe");
driver.FindElement(By.Id("email")).SendKeys("john.doe@example.com");
driver.FindElement(By.Id("phone")).SendKeys("555-123-4567");
// Password with confirmation
driver.FindElement(By.Id("password")).SendKeys("SecurePass123!");
driver.FindElement(By.Id("confirmPassword")).SendKeys("SecurePass123!");
// Radio button - gender
driver.FindElement(By.CssSelector("input[value='male']")).Click();
// Checkboxes
IWebElement newsletter = driver.FindElement(By.Id("newsletter"));
if (!newsletter.Selected)
{
newsletter.Click();
}
IWebElement terms = driver.FindElement(By.Id("terms"));
if (!terms.Selected)
{
terms.Click();
}
// Textarea
driver.FindElement(By.Id("bio")).SendKeys(
"Software developer with 5 years of experience."
);
// Submit
driver.FindElement(By.Id("submit-btn")).Click();
}

Best Practices

  1. Always clear before typing if the field might have existing content
  2. Check state before clicking checkboxes and radio buttons
  3. Use explicit waits after form submission
  4. Verify form validation messages appear correctly
  5. Test both valid and invalid inputs

Next Steps