How to Input Text in Selenium using SendKeys

Learn how to input text in Selenium using SendKeys with syntax, examples, advantages, limitations, and best practices.

Written by Sarthak Sharma Sarthak Sharma
Reviewed by Bhumika Babbar Bhumika Babbar
Last updated: 13 August 2026 8 min read

Key Takeaways

  • sendKeys is a built-in Selenium WebDriver method that types text into input fields by dispatching native browser events for individual characters.
  • Locate the target web element using a stable locator and pass your desired text string or functional Keys constants directly into the sendKeys method.
  • Always clear pre-filled text and wait explicitly for the input element to become interactable before invoking sendKeys to prevent test flakiness.

Whether it is validating login authentication, adding payment details or clicking on a call widget, testing input text is crucial for a responsive website UI.

If you are using the Selenium Interface, you can test and verify multiple web components right from your test case. Selenium uses Sendkeys as a built-in concept to input text and validate elements.

Each web page in Selenium has a different method with which it fetches input text, depending on the use case and the complexity of the test environment.

In this article, we will learn how to input text in Selenium with SendKeys for automated web app testing.

Different Methods to Input Text in Selenium

Selenium offers multiple techniques for entering text into fields. Each method comes with its own syntax, strengths, and limitations.

  • The SendKeys method directly types text into input fields by simulating keystrokes.
  • The Robot Class method automates low-level keyboard events, which is useful when handling OS-level popups or JavaScript prompts.
  • The JavaScript Executor method executes JavaScript to set text values directly in the DOM, which helps bypass limitations of traditional methods.
  • The Action Class Chains method simulates complex user interactions such as keypress sequences or mouse-and-keyboard combinations.

What is SendKeys in Selenium?

Sendkeys is a built-in Selenium WebDriver method that emulates user typing into a web wrapper or a web element, to validate whether the value entered is accurate or not.

By sending characters one at a time, it closely mimics the way a human would type, click, listen or view web components on an SPA (Single web page).

It maps fields by label, i.e., input text area, number, image or click, to trigger those actions in the backend while you run your UI test suite or test case.

Why Use SendKeys in Selenium?

Here are the primary reasons to use sendKeys in Selenium:

  • It mimics real user behaviour by simulating actual keyboard keystrokes.
  • It ensures compatibility across multiple browsers supported by Selenium WebDriver.
  • It works seamlessly with most input fields without requiring additional configurations.
  • It supports special keys such as ENTER, TAB, or BACKSPACE by leveraging the Keys class.

How Do Selenium SendKeys Work?

Rather than instantly pasting text into the DOM, Selenium sendKeys triggers the native OS and browser-level event pipelines.

When a test script executes sendKeys, the following sequence happens:

  • Element Focus Verification: Selenium WebDriver first checks if the target element is visible and interactable in the viewport. If the element is hidden, disabled, or overlapped by an overlay (like a modal or sticky banner), the driver throws an error, like InvalidElementStateException.
  • Native Event Dispatching: SendKeys dispatches individual events for every character in sequence. For example, keydown is for when a key is pressed down, keypress is for when a key is active, oninput is for when DOM is executed, and keyup is for when a key is released.
  • Triggering Client-Side Framework Observers: Because sendKeys fires native browser events, the test automation framework immediately detects the input. Client validation scripts (e.g., format checkers, character counters or search popups) are triggered in the backend, mimicking a user.
  • Handling Non-Printable and Special Keys : By passing constants from the Keys enum (such as Keys.ENTER, Keys.TAB, or Keys.BACK_SPACE), sendKeys triggers specific virtual key code combinations (keyCode / which properties in event payloads) recognised by the browser engine.

SendKeys ensures compatibility across multiple browsers supported by Selenium WebDriver.

When testing across complex environments, running scripts on a cloud-based Selenium Grid guarantees that these native keystroke events behave consistently across legacy and modern browser versions.”

How to Input Text in Selenium Using SendKeys: Example

Here is a simple example where sendKeys() is used to input text into a login form

import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SendKeysExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get(“https://example.com/login”);

// Locate username field
WebElement username = driver.findElement(By.id(“username”));
username.sendKeys(“testuser”);

// Locate password field
WebElement password = driver.findElement(By.id(“password”));
password.sendKeys(“securePass123”);

// Submit form
password.submit();

driver.quit();
}
}

This script launches a browser, navigates to a login page, inputs credentials, and submits the form.

Advantages of Using SendKeys for Input Text in Selenium

Here are the advantages of using SendKeys for input text in Selenium:

  • The method is simple and intuitive, requiring minimal code to implement.
  • It works effectively with most standard HTML input elements.
  • It can handle both normal text input and keyboard actions such as TAB or ENTER.
  • It is easy to maintain and widely documented, making it accessible for testers at all skill levels.

Limitations of Using SendKeys for Input Text in Selenium

Here are the limitations of using SendKeys for input text in Selenium:

  • SendKeys may fail to work properly with non-standard input fields, especially those built with complex JavaScript frameworks.
  • It can be slower compared to directly setting values through JavaScript Executor.
  • It cannot be used for OS-level interactions such as handling file upload dialogs.
  • It may encounter synchronization issues if the element is not fully loaded before input.

What to Be Careful of Before Inputting Text in Selenium

If you are trying to fetch a value that’s not specifically defined in the test script, your Selenium console will throw an exception.

InvalidElementStateException is one of the most common errors. It occurs when the element exists in the DOM but cannot accept input. Possible causes include the element being disabled or marked as readonly, the element not being visible or interactable, or another element overlapping the input field.

Solution:

  • Testers can use explicit waits such as ExpectedConditions. elementToBeClickable to ensure the element is clickable before sending text.
  • Testers should verify that the correct locator is being used to avoid targeting the wrong element.
  • Testers need to confirm that the input field is designed to accept user input and is not blocked by front-end restrictions.

Best Practices to Input Text in Selenium

Selenium tests still cannot provide the full picture of whether an app works or not. To ensure you do not bump into any runtime exception, here are the best practices to follow:

  • Testers should always wait for elements to be visible or clickable before attempting to interact with them.
  • Testers should use strong locators such as id, name, CSS selector, or XPath to ensure stability.
  • Testers should avoid hard-coded sleep statements and instead use explicit waits for synchronization.
  • Testers should validate input values after sending text by retrieving field values with getAttribute(“value”).
  • Testers can use JavaScript Executor as a fallback if standard methods fail to input text.
  • Testers should use Action Class Chains for scenarios that require complex multi-step interactions involving both keyboard and mouse actions.

Why Run Selenium Tests on BrowserStack Automate?

Selenium tests executed only on local machines cannot provide a complete picture of application behavior, since websites often respond differently across browsers, operating systems, and device types.

BrowserStack Automate addresses this limitation by providing a cloud-based infrastructure where tests can be run on thousands of real devices and browsers. This ensures that teams validate functionality under real-world conditions without the overhead of managing in-house device labs.

  • BrowserStack Automate provides access to over 3500 real browsers and devices, ensuring that applications are tested under the same conditions experienced by end users.
  • It enables parallel test execution, which significantly reduces overall test execution time and allows faster feedback cycles.
  • It allows QA teams to simulate real-world scenarios such as varying network conditions and geolocation, which ensures accurate performance validation.
  • It eliminates the need to maintain and update internal device labs, which reduces infrastructure costs and resource management overhead.
  • It ensures seamless integration with popular CI/CD pipelines, enabling teams to run automated tests as part of their continuous delivery process.

Conclusion

Using pre-built modules like SendKeys, you can validate user actions across various browsers and platforms that won’t hurt your product ROI in the long run.

SendKeys also prevents the potential of flaky tests in CI/CD by giving a deterministic call to the DOM and fetching values fast.

While sendKeys remains the most straightforward and widely used method, other techniques such as Robot Class, JavaScript Executor, and Action Class Chains provide flexibility for advanced scenarios.

Running these Selenium tests on BrowserStack Automate further guarantees that applications deliver consistent user experiences across browsers and devices.

Version History

  1. Aug 11, 2026 Current Version

    Added practical text-input automation workflow with Selenium, clarified when and how to use sendKeys(), and strengthened troubleshooting and best practices for reliable input handling.

    Bhumika Babbar
    Reviewed by Bhumika Babbar Principal Engineer
Tags
Automation Testing Selenium
Sarthak Sharma
Sarthak Sharma

Senior Software Development Engineer

Sarthak Sharma is a Senior Software Development Engineer with 9+ years of experience in software testing and customer engineering. He specializes in helping teams adopt effective automation practices and maximize the value of their testing infrastructure.

Automation Tests on Real Devices & Browsers
Seamlessly Run Automation Tests on 3500+ real Devices & Browsers