The sendKeys() method is one of the most frequently used methods in Selenium WebDriver for Java. It allows you to simulate keyboard input, just like a real user would type into a text field or form element in the browser. Whether you’re entering a username, password, search term, or any form data, sendKeys() is the go-to method.
What is sendKeys() method in Selenium?
The sendKeys() method is defined in the WebElement interface in Selenium. It simulates typing characters into an input field, textarea, or even sending keystrokes like ENTER
, TAB
, etc. It behaves the same way as physical keyboard input would when a user interacts with the browser.
Syntax:
void sendKeys(CharSequence... keysToSend);
- Parameters: One or more characters/sequences to send to the element.
- Returns: Nothing (void method).
- Defined in:
org.openqa.selenium.WebElement
Common Use Cases
- Typing in text fields (username, password)
- Filling out form elements
- Sending special keys like Keys.ENTER, Keys.TAB
- Simulating keyboard events for automation
Example: Typing Text in a Form Input
Here’s a basic Selenium test that opens a website and enters credentials in a login form:
import org.openqa.selenium.By; import org.openqa.selenium.Keys; 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) { // Set ChromeDriver path System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // Initialize the WebDriver WebDriver driver = new ChromeDriver(); // Open a website driver.get("https://www.example.com/login"); // Locate the username field and type text WebElement usernameField = driver.findElement(By.id("username")); usernameField.sendKeys("myUsername"); // Locate the password field and type text WebElement passwordField = driver.findElement(By.id("password")); passwordField.sendKeys("myPassword"); // Submit the form by pressing ENTER in the password field passwordField.sendKeys(Keys.ENTER); // Optional: close browser after the test driver.quit(); } }
Sending Special Keys
You can also send special keys using the Keys
class from org.openqa.selenium.Keys
. For example:
element.sendKeys(Keys.TAB); // Presses Tab key
element.sendKeys(Keys.ENTER); // Presses Enter key
element.sendKeys(Keys.CONTROL + "a"); // Select all text (Ctrl + A)
Note: You may need to wait for the element to be clickable or visible before sending keys.
Tips & Best Practices
1. Use the clear()
method before sendKeys()
if the input field may already contain text:
element.clear();
element.sendKeys("New text");
2. Use explicit waits (like WebDriverWait
) when dealing with dynamically loaded elements.
3. Be aware that sendKeys()
may not work on non-input elements or elements hidden by JavaScript.
4. Use Keys
enum for special keyboard interactions.
Common Mistakes
Mistake | Why it’s a Problem | Solution |
---|---|---|
Sending keys to non-input elements | Will throw an exception | Make sure the WebElement is a valid input |
Not using waits | Element not found or not interactable | Use WebDriverWait |
Not clearing input before typing | Old text remains in input field | Use element.clear() before sendKeys() |
Using wrong locator | ElementNotFound or NoSuchElementException thrown | Double-check locators using browser dev tools |