Selenium Send Keys - All You Need to Know

Using Send keys in Selenium

Selenium is a powerful tool that allows you to perform an automated test on your web applications. Features like Selenium Send Keys make Selenium extremely versatile, allowing input testing of a web page. The Sendkeys method lets you simulate an environment and test how your webpage reacts to the user’s input.  This makes the amount of menial work needed significantly less, making it an extremely popular testing tool. 

According to a report by enlyft, 78,921 companies use Selenium worldwide. Selenium holds a share of 27.35% in the software testing tools market. Do you wish to learn further about Sendkeys? Selenium Send Keys element method is used to enter text into fields that accept editable text on a website. Read further to learn more about Selenium Send Keys.   

Table of contents 

1) What is Selenium Send Keys? 

2) Uses of Sendkeys in Selenium 

3) How to use Sendkeys in Selenium? 

   a) Using Sendkeys in Selenium with Java 

   b) Using Sendkeys in Selenium with Python 

   c) Using Sendkeys in Selenium to erase text 

4) Troubleshoot with Sendkeys 

5) Conclusion 

What is Selenium Send Keys?   

Selenium Sendkeys are methods used to help you automate the testing process for input compatibility of a web application. It is a part of the Web Driver element used for webpage and web application testing. Selenium Web driver allows these methods to provide input in editable fields within the web application.  

The Send keys method’s syntax is written as sendKeys() in Java and send_keys() in Python. Generally, the steps to using the Send Keys method are as follows. 

1) Loading the webpage: The first step is to open the webpage you wish to test. The syntax to fetch this webpage in Selenium using Java is driver.get(); and the syntax in Python is driver.get(), respectively. 

2) Element identification: The next step is finding the element you wish to select. To locate an element, you can utilise the element locators present in Selenium, such as XPath or element ID. 

3) Keyboard input: Once you have located the elements, you can use a program script to send inputs to that element. Once you provide inputs to Selenium, you can continue with your remaining tasks. 

Try Introduction To Test Automation With Selenium Web Driver to master Sendkeys today! 

Uses of Sendkeys in Selenium 

Selenium Sendkeys are commonly used for various forms of input testing automation. There are some common input test use cases which utilise Sendkeys methods. These use cases for Sendkeys are as follows. 

1) Validation of input: Forms are a common sight in web applications. These forms constitute various input fields to collect a user’s data. An example of this would be the email address. Many of these web applications use a validation system to test if the email and password are correct. These validation systems work by testing whether a user’s input follows the general format rule. An example of this would be yourname@domainname format as a general rule.

These validations are necessary to avoid potential future errors. As these validation systems rely on an input to work, Sendkeys are the perfect method to test them. By providing multiple inputs that either obey or disobey the format, you can have versatile test cases for these systems. 

2) Search results evaluation: Search bars are common in any good web application and web page. This feature finds relevant results across a page based on a keyword. This keyword is decided and inserted by the user surfing the page. Since these search results depend on user input, it's the perfect opportunity to use Sendkeys.

Using Sendkeys, in this instance, allows you to test the accuracy of your search results. The quality of search results can be validated to further improve the search function and search algorithm. 

3) Testing special functions: Sendkeys usage in Selenium is not limited to simple text fields. A lot of web pages and web applications have special functions to them. Take Google Meet as an example. Google Meet has certain unique functions like mute and unmutes to it. This function is triggered by pressing CTRL+D in Google Meet.

Functions similar to these, on any webpage which relies on user input are a good place for input testing. This makes it an ideal use case for the Sendkeys method. 

Try our course in Selenium WebDriver With Python Training today! And master Sendkeys! 

How to use Sendkeys in Selenium? 

Since you know Selenium Sendkeys and their use cases, let's go through how to use them. This section of the blog will provide you with the program for Selenium test scripts. Selenium testing automation is extremely versatile; as a result, it is available in multiple programming languages. In this section, you will find programming scripts using the Sendkeys function in different programming languages. 

Selenium WebDriver With Python Training

Using Sendkeys in Selenium with Java 

Here is a Java program for using Sendkeys in Selenium. This program creates a new instance for the driver, in this case, it is Google Chrome. The syntax for all browsers is unique and is not limited to chrome, and the syntax for Goggle Chrome browser is ChromeDriver.    

Next, you need to navigate to the webpage you wish to test. Once you have navigated the webpage, use element locators to find the element you wish to test. Here the program uses findElement() method to find the input field by its ID. Then you can move on to using sendKeys() and enter the necessary input before running the script. 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 


import org.openqa.selenium.By; 

public class ExampleSendKeys { 

   public static void main(String[] args) { 

      // Set the path to the Chrome driver 

      System.setProperty("", "path/to/chromedriver");

      // Create a new instance of the Chrome driver 

      WebDriver driver = new ChromeDriver();

      // Navigate to a website 


      // Find the search box element by its name attribute 

      WebElement searchBox = driver.findElement("q"));

      // Enter text into the search box using Sendkeys 

      searchBox.sendKeys("Java selenium Send-Keys Example"); 

      // Submit the search by pressing Enter key 


      // Close the browser in selenium 



Using Sendkeys in Selenium with Python 

Here is a Python program for using Sendkeys in Selenium. It is similar to the Java program shown previously. This program navigates to a page you wish to test in Selenium. Next, it uses an element locator find_element_by_name() to find the field you wish to test on the page. Once the input field has been retrieved, send_keys() function will allow you to give inputs to test the input fields. Finally, the program will close the window once it provides input. 

from selenium import webdriver  

# create a new Google Chrome browser instance 

browser = webdriver.Chrome()

# navigate to a web page 


# find the text field element by its name attribute 

text_field = browser.find_element_by_name("username")

# enter text into the text field 


# close the browser in selenium 


Using Sendkeys to Selenium with erase text 

Erasing texts right after you input them, might seem slightly redundant, but it has a valid reason behind it. You’ll often witness a situation where you get suggestions before even typing anything in a search engine, for example, Google.    

This is often the result of the searches you’ve made. It's normal to open a URL many times and freshly type it, but is that convenient or efficient? Not only are the underhanded methods to get a fresh search time-consuming, it burdens the system. Here are some programs to help you erase text with Sendkeys in different languages. 

Erasing texts with Sendkeys in Java: 

import org.openqa.selenium.By; 

import org.openqa.selenium.Keys; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 


public class TextEraser {  

    public static void main(String[] args) { 

        // Set the path to the Chrome driver executable 

        System.setProperty("", "path/to/chromedriver"); 

        // Create an instance of the Chrome driver 

        WebDriver driver = new ChromeDriver(); 

        // Navigate to the web page containing the input field 


        // Locate the input field element 

        WebElement inputField = driver.findElement("input-field-id"));

        // Clear the text using the backspace key 

        inputField.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE); 

        // Close the browser in selenium 



Erasing texts with Sendkeys in Python: 

from selenium import webdriver 

# Create a new instance of the Chrome driver 

driver = webdriver.Chrome() 

# Navigate to a web page 


# Find the element where the text is entered and clear the existing text 

text_element = driver.find_element_by_xpath("//input[@name='username']") 


# Enter new text into the element 

text_element.send_keys("new text") 

# Close the browser in selenium 


Wish to use Sendkeys method in different languages? Try Selenium Immersion With C#! 

Troubleshoot with Sendkeys 

Sendkeys have added benefit of letting you give dummy input to perform automated tests on web pages. This may seem simple; however, it takes time to master, especially for novice Selenium users. Here we will see some common grounds for errors that take place within Selenium sendkeys functions and how to avoid them.   

Starting with the input type, you need to verify the type of input you are testing. An example is using an image as an element to use Selenium Sendkeys, and this will make your script return errors despite multiple tries. Sendkeys are strictly limited to inputs that the keyboard can insert, and another common issue might be using incorrect references with the Sendkeys.   

This problem commonly persists in web pages and applications where elements are nested together. You must identify the said element and use the correct reference to it. The reference for an element can be an ID, class, name etc within the nested elements.  

Another scenario for a common error is event triggered element. For example, certain elements are created to take input, but only accept it after “Enter” is pressed or the mouse is clicked. An example of this is Textbox, which needs a mouse click to accept the input. Simply include a block of code to trigger such an event after locating the elements, followed by the Sendkeys method. This will let your element accept input as usual.  

Lastly, certain elements can be inactive and have prerequisites to activate the elements. Such as a text field that only unlocks after the previous text field has been filled. Directly giving input to such a field lead to error. So, fill in the previous fields first to activate the necessary field, followed by the Sendkeys method.   


Selenium Send Keys allows you to send keyboard inputs using Selenium automation tool. Sendkeys allow you to conveniently navigate, locate and test for input fields in Selenium web driver. With this blog, you’ll hopefully have a much better understanding of the Sendkeys method, and its syntax in different programming languages. You'll be able to use Sendkeys method and troubleshoot its problem more efficiently. Thank you for reading. 

Interested in learning more? Try our course in Selenium Testing Framework Training!


Back to top