Forms and Inputs
Master form interactions including text fields, checkboxes, radio buttons, and form submission.
Selenium 3 & 4 Stable Verified against Selenium 4.48.0
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 entryWebElement usernameField = driver.findElement(By.id("username"));usernameField.sendKeys("testuser");
// Clear existing text firstWebElement emailField = driver.findElement(By.id("email"));emailField.clear();emailField.sendKeys("test@example.com");
// Get current valueString currentValue = emailField.getAttribute("value");System.out.println("Current value: " + currentValue);
// Check if field is enabledboolean isEnabled = emailField.isEnabled();
// Check if field is displayedboolean isVisible = emailField.isDisplayed();
// Password fields work the same wayWebElement 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 Byfrom selenium.webdriver.common.keys import Keys
# Basic text entryusername_field = driver.find_element(By.ID, "username")username_field.send_keys("testuser")
# Clear existing text firstemail_field = driver.find_element(By.ID, "email")email_field.clear()email_field.send_keys("test@example.com")
# Get current valuecurrent_value = email_field.get_attribute("value")print(f"Current value: {current_value}")
# Check if field is enabledis_enabled = email_field.is_enabled()
# Check if field is displayedis_visible = email_field.is_displayed()
# Password fields work the same waypassword_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 entryconst usernameField = await driver.findElement(By.id('username'));await usernameField.sendKeys('testuser');
// Clear existing text firstconst emailField = await driver.findElement(By.id('email'));await emailField.clear();await emailField.sendKeys('test@example.com');
// Get current valueconst currentValue = await emailField.getAttribute('value');console.log(`Current value: ${currentValue}`);
// Check if field is enabledconst isEnabled = await emailField.isEnabled();
// Check if field is displayedconst isVisible = await emailField.isDisplayed();
// Password fields work the same wayconst 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 entryIWebElement usernameField = driver.FindElement(By.Id("username"));usernameField.SendKeys("testuser");
// Clear existing text firstIWebElement emailField = driver.FindElement(By.Id("email"));emailField.Clear();emailField.SendKeys("test@example.com");
// Get current valuestring currentValue = emailField.GetAttribute("value");Console.WriteLine($"Current value: {currentValue}");
// Check if field is enabledbool isEnabled = emailField.Enabled;
// Check if field is displayedbool isVisible = emailField.Displayed;
// Password fields work the same wayIWebElement 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 checkboxWebElement agreeCheckbox = driver.findElement(By.id("agree-terms"));
// Check if already selectedboolean isSelected = agreeCheckbox.isSelected();
// Select only if not already selectedif (!agreeCheckbox.isSelected()) { agreeCheckbox.click();}
// Uncheck only if currently checkedif (agreeCheckbox.isSelected()) { agreeCheckbox.click();}
// Toggle checkbox (switch state)agreeCheckbox.click();
// Work with multiple checkboxesList<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type='checkbox']"));for (WebElement checkbox : checkboxes) { if (!checkbox.isSelected()) { checkbox.click(); // Select all }}
// Select checkbox by label textWebElement label = driver.findElement(By.xpath("//label[text()='Subscribe to newsletter']"));label.click(); // Clicking label toggles associated checkbox# Find checkboxagree_checkbox = driver.find_element(By.ID, "agree-terms")
# Check if already selectedis_selected = agree_checkbox.is_selected()
# Select only if not already selectedif not agree_checkbox.is_selected(): agree_checkbox.click()
# Uncheck only if currently checkedif agree_checkbox.is_selected(): agree_checkbox.click()
# Toggle checkbox (switch state)agree_checkbox.click()
# Work with multiple checkboxescheckboxes = 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 textlabel = driver.find_element(By.XPATH, "//label[text()='Subscribe to newsletter']")label.click() # Clicking label toggles associated checkbox// Find checkboxconst agreeCheckbox = await driver.findElement(By.id('agree-terms'));
// Check if already selectedconst isSelected = await agreeCheckbox.isSelected();
// Select only if not already selectedif (!(await agreeCheckbox.isSelected())) { await agreeCheckbox.click();}
// Uncheck only if currently checkedif (await agreeCheckbox.isSelected()) { await agreeCheckbox.click();}
// Toggle checkbox (switch state)await agreeCheckbox.click();
// Work with multiple checkboxesconst 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 textconst label = await driver.findElement(By.xpath("//label[text()='Subscribe to newsletter']"));await label.click(); // Clicking label toggles associated checkbox// Find checkboxIWebElement agreeCheckbox = driver.FindElement(By.Id("agree-terms"));
// Check if already selectedbool isSelected = agreeCheckbox.Selected;
// Select only if not already selectedif (!agreeCheckbox.Selected){ agreeCheckbox.Click();}
// Uncheck only if currently checkedif (agreeCheckbox.Selected){ agreeCheckbox.Click();}
// Toggle checkbox (switch state)agreeCheckbox.Click();
// Work with multiple checkboxesIList<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 textIWebElement label = driver.FindElement(By.XPath("//label[text()='Subscribe to newsletter']"));label.Click(); // Clicking label toggles associated checkboxRadio Buttons
Working with Radio Buttons
Selenium 3 & 4 Stable
// Select a specific radio button by valueWebElement maleRadio = driver.findElement( By.cssSelector("input[type='radio'][value='male']"));maleRadio.click();
// Check which radio is selected in a groupList<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 labelWebElement label = driver.findElement(By.xpath("//label[text()='Express Shipping']"));label.click();
// Or find radio by label's 'for' attributeWebElement radioById = driver.findElement( By.id(driver.findElement(By.xpath("//label[text()='Standard']")).getAttribute("for")));radioById.click();# Select a specific radio button by valuemale_radio = driver.find_element( By.CSS_SELECTOR, "input[type='radio'][value='male']")male_radio.click()
# Check which radio is selected in a groupgender_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 labellabel = driver.find_element(By.XPATH, "//label[text()='Express Shipping']")label.click()
# Or find radio by label's 'for' attributelabel_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 valueconst maleRadio = await driver.findElement( By.css("input[type='radio'][value='male']"));await maleRadio.click();
// Check which radio is selected in a groupconst 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 labelconst label = await driver.findElement(By.xpath("//label[text()='Express Shipping']"));await label.click();
// Or find radio by label's 'for' attributeconst 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 valueIWebElement maleRadio = driver.FindElement( By.CssSelector("input[type='radio'][value='male']"));maleRadio.Click();
// Check which radio is selected in a groupIList<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 labelIWebElement label = driver.FindElement(By.XPath("//label[text()='Express Shipping']"));label.Click();
// Or find radio by label's 'for' attributeIWebElement 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 buttonWebElement submitButton = driver.findElement(By.cssSelector("button[type='submit']"));submitButton.click();
// Method 2: Use submit() on form or any form elementWebElement form = driver.findElement(By.id("login-form"));form.submit();
// Or submit from any element within the formWebElement usernameField = driver.findElement(By.id("username"));usernameField.submit(); // Submits the parent form
// Method 3: Press Enter key in a fieldWebElement passwordField = driver.findElement(By.id("password"));passwordField.sendKeys("mypassword" + Keys.ENTER);
// Wait for form submission to completeWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));wait.until(ExpectedConditions.urlContains("/dashboard"));from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC
# Method 1: Click submit buttonsubmit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")submit_button.click()
# Method 2: Use submit() on form or any form elementform = driver.find_element(By.ID, "login-form")form.submit()
# Or submit from any element within the formusername_field = driver.find_element(By.ID, "username")username_field.submit() # Submits the parent form
# Method 3: Press Enter key in a fieldpassword_field = driver.find_element(By.ID, "password")password_field.send_keys("mypassword" + Keys.ENTER)
# Wait for form submission to completewait = WebDriverWait(driver, 10)wait.until(EC.url_contains("/dashboard"))const { until } = require('selenium-webdriver');
// Method 1: Click submit buttonconst submitButton = await driver.findElement(By.css("button[type='submit']"));await submitButton.click();
// Method 2: Use submit() on form or any form elementconst form = await driver.findElement(By.id('login-form'));await form.submit();
// Or submit from any element within the formconst usernameField = await driver.findElement(By.id('username'));await usernameField.submit(); // Submits the parent form
// Method 3: Press Enter key in a fieldconst passwordField = await driver.findElement(By.id('password'));await passwordField.sendKeys('mypassword', Key.ENTER);
// Wait for form submission to completeawait driver.wait(until.urlContains('/dashboard'), 10000);using OpenQA.Selenium.Support.UI;
// Method 1: Click submit buttonIWebElement submitButton = driver.FindElement(By.CssSelector("button[type='submit']"));submitButton.Click();
// Method 2: Use submit() on form or any form elementIWebElement form = driver.FindElement(By.Id("login-form"));form.Submit();
// Or submit from any element within the formIWebElement usernameField = driver.FindElement(By.Id("username"));usernameField.Submit(); // Submits the parent form
// Method 3: Press Enter key in a fieldIWebElement passwordField = driver.FindElement(By.Id("password"));passwordField.SendKeys("mypassword" + Keys.Enter);
// Wait for form submission to completeWebDriverWait 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
- Always clear before typing if the field might have existing content
- Check state before clicking checkboxes and radio buttons
- Use explicit waits after form submission
- Verify form validation messages appear correctly
- Test both valid and invalid inputs
Next Steps
- Dropdowns and Selects - Work with select elements
- Keyboard Actions - Advanced keyboard interactions
- Explicit Waits - Wait for form changes
Copy-paste recipes for this topic
- Clear and Type Into a React or Angular Input Reliably
Replace the value of a framework-controlled input when clear() does not stick, using select-all plus type and the native setter fallback.
- Set a Date in a Date Picker
Type into the input when allowed, set the value directly when it is read-only, or drive the calendar widget when nothing else works.