We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Simulating keyboard typing into form fields in Selenium WebDriver.
The sendKeys() command tells Selenium to type characters into an element as if a user is using the keyboard.
<input> elements (text, email, password, etc.)<textarea>contenteditable="true"id) when possible. Prefer to clear the field first before typing, so tests are deterministic.Element ID: lesson4-input
Current value: (empty)
Type into the input field and watch the "Current value" update. This mimics how Selenium sends keys to the element.
12345WebElement usernameInput = driver.findElement(By.id("lesson4-input"));
usernameInput.clear();
usernameInput.sendKeys("Hello World");
driver.quit();id."Hello World" into the input, character by character.For <input> and <textarea>, always use getAttribute("value") to read what’s inside—getText() won’t work.
sendKeys() accepts special keys: sendKeys("Hello", Keys.ENTER) types "Hello" then presses Enter.
Combine keys like Keys.CONTROL, "a" to Select All, Copy, or Paste for richer interactions.