sendKeys() not working in Selenium Webdriver
Last Updated :
23 Jul, 2025
In the world of Selenium WebDriver automation, the sendKeys()
method is commonly used to input text into web elements like text fields and search boxes. However, there are times when sendKeys()
might not work as expected, leading to challenges in test automation. This issue can arise due to various reasons such as element visibility, focus issues, or JavaScript interference. Understanding these issues and exploring alternative methods, such as using JavaScript Executor, is crucial for effective test automation and ensuring that your tests run smoothly.
This article will delve into why sendKeys()
might fail and how you can address these issues.
Understanding sendKeys()
The sendKeys() method is used in Selenium to input text into web elements. It simulates keyboard input, allowing you to interact with text fields, password fields, and text areas on a webpage. The basic syntax is:
Java
WebElement element = driver.findElement(By.id("elementId"));
element.sendKeys("text to input");
Common Issues with sendKeys()
1. Element Not Interactable
- Issue: If an element is not interactable, sendKeys() will fail. This might occur if the element is not yet visible or enabled.
- Solution: Ensure the element is visible and enabled before attempting to send keys.
Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));
element.sendKeys("text to input");
2. Element Not Found
- Issue: If the element is not found, sendKeys() will not work.
- Solution: Verify the locator used to find the element. Use reliable locators and ensure they are correct.
Java
WebElement element = driver.findElement(By.id("elementId"));
element.sendKeys("text to input");
3. Element is Covered by Another Element
- Issue: If the element is covered by another element, sendKeys() will fail because the underlying element is not clickable.
- Solution: Ensure no other element is overlapping the target element. You may need to scroll or adjust the page layout.
Java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.sendKeys("text to input");
- Issue: Some inputs are managed by JavaScript and may not respond to sendKeys().
- Solution: Use JavaScript to set the value of such elements.
Java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].value='text to input';", element);
5. Stale Element Reference
- Issue: If the page is refreshed or the element is updated, a stale element reference exception may occur.
- Solution: Re-locate the element if it becomes stale.
Java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].value='text to input';", element);
6. Solutions and Workarounds
- Wait for Element to be Visible
- Using WebDriverWait to ensure the element is interactable can prevent many issues.
Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
element.sendKeys("text to input");
- Use Actions Class for Complex Interactions
- The Actions class can be used for more complex interactions that might involve multiple steps.
Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.moveToElement(element).click().sendKeys("text to input").perform();
7. Handle Overlapping Elements
If elements overlap, use JavaScript to interact with the element.
Java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
element.sendKeys("text to input");
Use JavaScript Executor for JavaScript-Based Inputs
For JavaScript-managed inputs, set the value directly using JavaScript.
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].value = 'text to input';", element);
Refresh the WebElement
Re-locate the element if it becomes stale or if you encounter issues with element state.
Java
WebElement element = driver.findElement(By.id("elementId"));
element.sendKeys("text to input");
// Handle stale element
element = driver.findElement(By.id("elementId"));
element.sendKeys("text to input");
Example of solving Problem sendKeys() not working in Selenium Webdriver
Here’s a complete example demonstrating how to handle common issues with sendKeys():
Java
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SendKeysExample {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
try {
// Launch the Google homepage
driver.get("https://www.google.com/");
// Slow down to observe the page loading
Thread.sleep(2000); // Wait for 2 seconds
// Identify the search box element
WebElement searchBox = driver.findElement(By.name("q"));
// Slow down to observe the search box before entering text
Thread.sleep(2000); // Wait for 2 seconds
// Use JavaScript Executor to enter text into the search box
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementsByName('q')[0].value= 'geeksforgeeks.org'");
// Slow down to observe the entered text
Thread.sleep(2000); // Wait for 2 seconds
// Retrieve the entered text to verify
String enteredText = searchBox.getAttribute("value");
System.out.println("Text entered: " + enteredText);
// Slow down before closing the browser
Thread.sleep(2000); // Wait for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Output:
SendKeysAlternate outputConclusion
In summary, while sendKeys()
is a fundamental method in Selenium WebDriver for simulating user input, it is not always reliable in every scenario. Issues with element visibility, focus, or JavaScript interactions can cause sendKeys()
to malfunction. By leveraging alternative approaches, such as using JavaScript Executor to directly set the value of input fields, you can overcome these challenges and enhance the robustness of your automated tests.
Understanding and addressing these issues ensures that your Selenium WebDriver tests remain effective and dependable, leading to more reliable automation outcomes
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING