Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Selenium WebDriver: Understanding the sendKeys() Method in Java

Posted on July 21, 2025July 21, 2025 By Admin No Comments on Selenium WebDriver: Understanding the sendKeys() Method in Java

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

MistakeWhy it’s a ProblemSolution
Sending keys to non-input elementsWill throw an exceptionMake sure the WebElement is a valid input
Not using waitsElement not found or not interactableUse WebDriverWait
Not clearing input before typingOld text remains in input fieldUse element.clear() before sendKeys()
Using wrong locatorElementNotFound or NoSuchElementException thrownDouble-check locators using browser dev tools
Selenium Tags:Selenium-Java

Post navigation

Previous Post: Selenium WebDriver in Java: Difference Between close() and quit() Methods
Next Post: How to Insert Image in HTML?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme