Skip to content
CodeKruPro

Clear and Replace Text

18/70

Video walkthrough

Watch on YouTube

Clear and Replace Text

The clear() method removes all text from an input field. Combine with sendKeys() to replace existing text.

On the Text Fields page, field #8 (ID: preFilledTextField) has a pre-filled value of "Codekru".

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the pre-filled field
  3. Print its initial value
  4. Clear the field
  5. Type Selenium
  6. Print its new value

Expected output:

Before: Codekru
After: Selenium

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

  1. Read the field's current value and print it after the label Before:.
  2. Call clear() on the field.
  3. Type the replacement word from the task with sendKeys().
  4. Read the value again and print it after the label After:.

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. Check isEnabled() and the readonly attribute first if the field's state can vary.

  • Relying on clear() in every framework

    clear() fires change but no input event, and React and some Angular inputs listen for input, so their own copy of the value is not updated. If the old text comes back, select all with Keys.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.

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.

Main.java
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();
    }
}