About this lesson
Type text into an input with sendKeys in Selenium Java
Selenium Java AssignmentsBeginnerWeb Element Interactions
How it works
sendKeys() focuses the element and types characters into it one key event at a time, so input handlers, autocomplete and validation all see the typing. It takes any number of CharSequence arguments, which means you can mix text and special keys: element.sendKeys("qa@example.com", Keys.TAB) types an address and then moves focus to the next field.
Typing adds to whatever is already there. Sending one more character to the pre-filled field on this page leaves the old text in place with the new character after it.
You cannot read typed text with getText(). An input has no visible text content, so getText() returns an empty string. What the user typed lives in the field's live value, which getAttribute("value") or getDomProperty("value") return.
About the assignment
Text Fields starts with an empty first-name input. You type a word into it and then read the field back, the same way a test checks that a form field kept the input.
What you will do
- Find the first-name field by the id from the task.
- Type the word from the task into it with
sendKeys(). - Read the
valueattribute withgetAttribute()and print it after the labelEntered:.
Common mistakes
Typing into a hidden or disabled field
Both throw
ElementNotInteractableException: element not interactable. On this page that applies toinvisibleFieldanddisabledField.Passing null
sendKeys(null)fails before anything reaches the browser, withIllegalArgumentException: Keys to send must be set. This usually means a test-data variable was never filled in.Checking the typed text with getDomAttribute()
getDomAttribute("value")reads the attribute written in the HTML, which typing does not change. Here it returnsnullafter typing, whilegetAttribute("value")returns what was typed.Typing into the wrong duplicate
This page has two inputs with
id="firstName".findElement()returns the first one, which is visible, so the task works, but the second field stays empty.
In a real test suite
Login forms, search boxes and checkout forms are all sendKeys() work. Teams keep the values in test-data files rather than in the test, and put Keys.ENTER at the end to submit a search the way a user would.
Lesson 16 clicked a button. Because typing appends, the next lesson, 18, shows how to replace the text in a field that already has some.
Related lessons
- Clear and Replace TextEmpty a field before typing so the new text replaces the old.
- Get Attribute Value with getAttribute()More about reading attributes, and when
valuediffers from the HTML. - Keyboard - Special KeysSend key combinations such as select-all with
Keys.chord().
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");
// TODO: Find the firstName field
// TODO: Use sendKeys() to type "Selenium"
// TODO: Read the value back with getAttribute("value")
// TODO: Print in format: "Entered: <value>"
driver.quit();
}
}