Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

To type into a text box using Selenium, locate the input element with a locator such as ID, name, or CSS selector, then call the sendKeys() method with the text you want to enter. The sendKeys() method of the WebElement interface simulates real keystrokes, so it works for standard input fields, textareas, and most editable elements.
While the basic call is simple, robust automation also involves waiting for the field, clearing old values, sending special keys, and handling cases where sendKeys() does not behave as expected. This guide covers all of it.
In Selenium, every interaction with an input starts by finding the element and then acting on it. The sendKeys() method types character by character, mimicking a real user, which also triggers the JavaScript key and input events many modern web apps rely on. Here is a minimal Java example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TypeIntoTextBox {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
// Locate the text box and type into it
WebElement textBox = driver.findElement(By.name("my-text"));
textBox.sendKeys("Hello, World!");
driver.quit();
}
}Before you can type, you must reliably find the field. Selenium supports several locator strategies, and choosing a stable one is key to avoiding flaky tests:
For a deeper look at finding fields reliably, see how to locate elements on a web page and interact with them.
Because sendKeys() appends rather than replaces, call clear() first when a field may already contain text. You can also send special keys, such as ENTER or TAB, using the Keys enumeration, and combine text with keys in a single call:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
// Overwrite any existing value, then submit with Enter
WebElement search = driver.findElement(By.id("search-box"));
search.clear();
search.sendKeys("Selenium automation" + Keys.ENTER);
// Move focus to the next field with Tab
search.sendKeys(Keys.TAB);Wrapping the interaction in an explicit wait until the element is clickable prevents "element not interactable" errors on slow-loading pages.
When a field is hidden, read-only, or built with a custom component, sendKeys() may not work. JavaScriptExecutor lets you set the value directly on the element:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
WebElement field = driver.findElement(By.id("custom-field"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='Hello, World!';", field);Use this as a fallback rather than a default, since it bypasses the keystroke events a real user would trigger. To go deeper on the driver internals, review the WebDriver guide.
Text input behavior can differ subtly between browsers, especially with autofill, virtual keyboards on mobile, and IME handling. Validating your sendKeys() logic on just one local browser leaves those differences untested. With TestMu AI, you can run the same Selenium script across 3000+ real browsers, devices, and OS combinations in parallel, confirming that form input works consistently for every user.
Connecting your suite to a remote Selenium Grid on a cloud automation testing platform means no local browser maintenance, and pairing it with cross browser testing catches rendering and interaction bugs before release.
Typing into a text box in Selenium starts with a single sendKeys() call, but production-grade automation means locating the field reliably, clearing old values, sending special keys, and having a JavaScriptExecutor fallback for stubborn elements. Add explicit waits to avoid interaction errors, and run across real browsers and devices so your form automation behaves the same everywhere your users type.
The sendKeys() method of the WebElement interface is used to type into a text box in Selenium. After locating the input field with a locator such as ID, name, or CSS selector, you call sendKeys() with the string you want to enter, which simulates real keystrokes.
No, sendKeys() appends to whatever text is already in the field rather than replacing it. To overwrite an existing value, call the clear() method on the element first, or select the current text with a keyboard shortcut before typing the new value.
Use the Keys enumeration with sendKeys(), for example element.sendKeys(Keys.ENTER) or Keys.TAB. You can also combine text and keys, such as typing a search term followed by Keys.ENTER, to enter a value and submit the form in a single call.
Common causes include the element not being visible or interactable yet, an incorrect locator, or the field being disabled or read-only. Add an explicit wait until the element is clickable, verify the locator, and if the field is custom, use JavaScriptExecutor as a fallback.
Yes, you can use JavaScriptExecutor to set the value attribute directly on the element. This bypasses the normal keystroke simulation and is useful for hidden or non-standard fields, though it does not trigger the same JavaScript events a real user would.
Write the sendKeys() logic once and run it against a Selenium Grid or cloud that offers many browser and OS combinations. This confirms the input behavior is consistent across Chrome, Firefox, Safari, and Edge without maintaining local browsers.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance