Popular Searches
Popular Course Categories
Popular Courses

Input Fields

HTML & Web Elements

Selenium Input Fields

Input fields are one of the most common elements used in web applications. They allow users to enter information such as username, email address, password, phone number, search keywords, address, dates, and other form data. In Selenium WebDriver automation, input fields are handled using WebElement methods such as sendKeys(), clear(), click(), and other element interaction methods.

Selenium WebDriver provides automation capabilities for interacting with web form controls in a way that closely represents normal user interaction. The commonly used operations for input fields include locating the field, clicking it when required, entering text, clearing existing values, reading field values, and validating the field state.


1. What Are Input Fields?

An input field is an HTML form element that accepts information from the user. The most common HTML element used for input fields is the element.

For example:




Selenium identifies these elements using locators and then performs actions on the corresponding WebElement.

Basic Flow

Open Browser
     ↓
Open Web Page
     ↓
Locate Input Field
     ↓
Verify Field
     ↓
Click Field if Required
     ↓
Clear Existing Value
     ↓
Enter Data using sendKeys()
     ↓
Validate Entered Data
     ↓
Submit Form

2. Why Input Field Automation Is Important

Most web applications contain forms. Login pages, registration pages, checkout pages, contact forms, search pages, banking applications, booking applications, and administrative applications all depend heavily on input fields.

  • Automates repetitive data-entry operations.
  • Reduces manual testing effort.
  • Validates form functionality.
  • Tests required-field validation.
  • Tests invalid and valid input combinations.
  • Supports regression testing.
  • Helps verify application workflows.
  • Allows large numbers of test cases to be executed consistently.

3. Common Input Field Types

Input Type HTML Example Common Selenium Operation
Text sendKeys(), clear()
Password sendKeys(), clear()
Email sendKeys(), clear()
Number sendKeys(), clear()
Search sendKeys(), clear()
Telephone sendKeys(), clear()
URL sendKeys(), clear()
Date sendKeys() or application-specific handling
File sendKeys(filePath)
Checkbox click()
Radio Button click()

4. Locating an Input Field

Before interacting with an input field, Selenium must locate the element on the web page. Selenium supports different locator strategies such as ID, Name, Class Name, CSS Selector, XPath, Tag Name, Link Text, Partial Link Text, and other supported mechanisms.

Example HTML

Using ID

WebElement username = driver.findElement(By.id("username"));

Using Name

WebElement username = driver.findElement(By.name("username"));

Using CSS Selector

WebElement username = driver.findElement(By.cssSelector("#username"));

Using XPath

WebElement username = driver.findElement(By.xpath("//input[@id='username']"));

5. Entering Text into an Input Field

The most common method for entering text into an input field is sendKeys().

The sendKeys() method sends keyboard input to an interactable element such as a text field.

Example

WebElement username = driver.findElement(By.id("username"));
username.sendKeys("admin");

After execution, the input field contains:

admin

Complete Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class InputFieldExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com");

        WebElement username = driver.findElement(By.id("username"));
        username.sendKeys("admin");

        driver.quit();
    }
}

6. Entering Email Address

Email fields are generally represented using type="email".

Selenium example:

WebElement email = driver.findElement(By.id("email"));
email.sendKeys("[email protected]");

When testing email validation, both valid and invalid email values should be tested.

Valid Examples

[email protected]
[email protected]
[email protected]

Invalid Examples

test
test@
@example.com
test.example.com

7. Entering Password

Password fields are normally represented using:

Selenium can enter a password using sendKeys().

WebElement password = driver.findElement(By.id("password"));
password.sendKeys("Test@123");

For security reasons, real automation projects should avoid exposing sensitive credentials directly inside source code. Test credentials should preferably be stored using appropriate configuration or secret-management mechanisms.


8. Clearing an Input Field

If an input field already contains text, Selenium provides the clear() method to remove the existing editable value.

WebElement username = driver.findElement(By.id("username"));

username.clear();
username.sendKeys("newUser");

Typical Flow

Existing Value
      ↓
clear()
      ↓
Empty Field
      ↓
sendKeys()
      ↓
New Value

9. Clear and Enter New Value

A common automation requirement is to replace an existing value with a new value.

WebElement email = driver.findElement(By.id("email"));

email.clear();
email.sendKeys("[email protected]");

This approach is useful when an application automatically populates a field or when the same form is reused across multiple test cases.


10. Clicking an Input Field

Some applications require the field to receive focus before typing. Selenium can click the input field before using sendKeys().

WebElement username = driver.findElement(By.id("username"));

username.click();
username.sendKeys("admin");

In many normal input fields, Selenium can directly use sendKeys(), but clicking first can be useful when the application contains custom controls or JavaScript-driven behavior.


11. Checking Whether an Input Field Is Displayed

The isDisplayed() method can be used to determine whether the input field is visible on the page.

WebElement username = driver.findElement(By.id("username"));

if (username.isDisplayed()) {
    System.out.println("Username field is displayed");
}

Why Use isDisplayed()?

  • Verify that a form field is visible.
  • Validate conditional fields.
  • Check whether a popup contains an expected field.
  • Debug element visibility problems.

12. Checking Whether an Input Field Is Enabled

The isEnabled() method checks whether the element is enabled for interaction.

WebElement username = driver.findElement(By.id("username"));

if (username.isEnabled()) {
    System.out.println("Username field is enabled");
} else {
    System.out.println("Username field is disabled");
}

This is useful when applications enable or disable fields based on user selections or workflow conditions.


13. Checking Whether an Input Field Is Selected

isSelected() is mainly useful for selectable controls such as checkboxes and radio buttons. It is generally not the primary method for normal text input fields.

WebElement checkbox = driver.findElement(By.id("terms"));

if (checkbox.isSelected()) {
    System.out.println("Checkbox is selected");
}

14. Reading the Value from an Input Field

For many HTML input elements, the live entered value can be inspected through the element's value property. In Selenium, getAttribute("value") is commonly used for this purpose.

WebElement username = driver.findElement(By.id("username"));

username.sendKeys("admin");

String value = username.getAttribute("value");

System.out.println("Entered value: " + value);

Expected Output

Entered value: admin

15. Difference Between getText() and Input Value

Method Common Usage Input Field
getText() Reads visible text content Usually not used to retrieve a typed input value
getAttribute("value") Commonly used to inspect an input value Useful for standard form inputs
getDomProperty("value") Reads the current DOM property Useful when verifying the live value property
getDomAttribute("value") Reads the markup attribute May represent the original HTML attribute rather than the current typed value

Example

WebElement username = driver.findElement(By.id("username"));

username.sendKeys("admin");

String value = username.getDomProperty("value");

System.out.println(value);

16. Handling Textarea Fields

A textarea is used when users need to enter multiple lines of text.