About this lesson
Clear an input field and replace its text in Selenium Java
Selenium Java AssignmentsBeginnerWeb Element Interactions
How it works
clear() empties an editable input or textarea by resetting its value, and the browser fires a change event afterwards. It exists because sendKeys() adds to existing text. Without it, typing into a pre-filled field gives you the old and new text joined together.
clear() resets the live value, not the HTML. After clearing the pre-filled field on this page, getAttribute("value") returns an empty string, but getDomAttribute("value") still returns the default written in the markup. A test that checks the field must read the live value.
Only fields the user could edit can be cleared. The read-only uneditable field and the disabled field on Text Fields both throw InvalidElementStateException, and the hidden invisibleField throws ElementNotInteractableException.
About the assignment
The starter already holds the pre-filled field in a variable. You record what it starts with, wipe it, type a replacement and read it again, so the before and after lines show that the old text is gone rather than added to.
What you will do
- Read the field's current
valueand print it after the labelBefore:. - Call
clear()on the field. - Type the replacement word from the task with
sendKeys(). - Read the
valueagain and print it after the labelAfter:.
Common mistakes
Skipping clear()
Typing straight into the pre-filled field gives both words run together in the after line, and the output check fails.
Clearing a read-only or disabled field
Chrome refuses, and Selenium throws
InvalidElementStateException: invalid element state. CheckisEnabled()and thereadonlyattribute first if the field's state can vary.Relying on clear() in every framework
clear()fireschangebut noinputevent, and React and some Angular inputs listen forinput, so their own copy of the value is not updated. If the old text comes back, select all withKeys.chord()and delete it, which sends real key events.
In a real test suite
Edit-profile and settings pages load with saved data, so tests that change a value nearly always clear first. Many teams wrap the pair in one helper method that clears and then types, so no test forgets the first step.
Lesson 17 showed that typing appends. With input covered, lesson 19 turns to reading the text a page displays, using getText() on the Text Messages page.
Related lessons
- Type Text with sendKeys()The
sendKeys()basics this lesson builds on. - Keyboard - Special KeysClear stubborn fields with a select-all key chord.
- isEnabled() - Check Enabled StateCheck
isEnabled()ondisabledFieldbefore you try to clear or type into it.
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/TextFields");
WebElement field = driver.findElement(By.id("preFilledTextField"));
// TODO: Print the initial value
// TODO: Clear the field
// TODO: Type "Selenium"
// TODO: Print the new value
driver.quit();
}
}