Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to Type Into a Text Box Using Selenium?

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.

Understanding the sendKeys() Method

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

Locating the Text Box

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:

  • By.id: The fastest and most stable locator when the input has a unique, non-dynamic ID.
  • By.name: Useful for form fields that carry a name attribute.
  • By.cssSelector: Flexible and performant for targeting by class, attribute, or hierarchy.
  • By.xpath: Powerful for complex conditions or text-based matching, though more brittle.

For a deeper look at finding fields reliably, see how to locate elements on a web page and interact with them.

Clearing a Field and Sending Special Keys

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.

Typing Without sendKeys() Using JavaScriptExecutor

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.

Common Mistakes and Troubleshooting

  • Not clearing the field: Forgetting clear() causes new text to append to old values, producing incorrect input.
  • ElementNotInteractableException: The field is not visible or ready yet. Add an explicit wait until it is clickable before typing.
  • StaleElementReferenceException: The DOM re-rendered after you located the element. Re-find the element right before interacting.
  • Wrong or dynamic locator: Auto-generated IDs change between loads. Prefer stable attributes or relative CSS/XPath.
  • Typing into a disabled field: A read-only or disabled input silently ignores sendKeys(). Check the element state first.

Running Selenium Tests Across Browsers and Real Devices

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.

Conclusion

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.

Frequently Asked Questions

Which method is used to type into a text box in Selenium?

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.

Does sendKeys() clear existing text before typing?

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.

How do you send special keys like Enter or Tab in Selenium?

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.

Why is sendKeys() not working on some elements?

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.

Can you type into a text box without sendKeys() in Selenium?

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.

How do you type into a text box across multiple browsers?

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.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

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

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests