Selenium SendKeys: All You Need To Know

Selenium SendKeys: All You Need To Know

In this blog, we will find out how to use sendKeys() in Selenium. Prior to that, you will be able to use Selenium sendKeys to automate the process of sending keystrokes.

Selenium automation is not just about navigating and clicking through the links. Yes, you can use it to switch tabs, find broken links, or even drag and drop directly. But there is a lot more to it. An essential aspect of test automation is passing values to the text fields. In this article, we’ll delve into how Selenium helps us pass values to text fields using the Selenium sendKeys() method. Let us start on our journey to learn how to send keys in Selenium. 

What Is Selenium sendKeys() Method?

Selenium provides sendKeys() method to input content in editable text fields or password fields in a webpage. These fields are like the typical web elements present on the web page that can be identified using any of the Selenium locators. You can refer to our article on Selenium locators to learn more about the different locators you can use.

Let us understand the utility of the sendKeys() method through an example. Consider the below use-case:

  • Open the website > LambdaTest.
  • Locate the web element to enter the email address.
  • Enter your email address.
  • Click on the ‘Start Free Testing’ button.

Let us now see what the code for the above use-case would look like, and then we will understand the code line-by-line.

Java1

package sendkeys;

2

import org.openqa.selenium.By;

3

import org.openqa.selenium.WebDriver;

4

import org.openqa.selenium.chrome.ChromeDriver;

5

import org.testng.annotations.AfterClass;

6

import org.testng.annotations.BeforeClass;

7

import org.testng.annotations.Test;

8

9

public class SendkeysTest {

10

11

WebDriver driver;

12

13

    //Method to set up the browser and open the dummy website

14

    @BeforeClass

15

    public void setUp() {

16

17

            System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\drivers\\chromedriver.exe");

18

            driver = new ChromeDriver();

19

            driver.get("https://www.lambdatest.com");

20

            driver.manage().window().maximize();

21

    }

22

23

    @Test

24

    public void testSendkeys() {

25

            //Locate the email and use sendkeys to pass the email to the field

26

            driver.findElement(By.id("useremail")).sendKeys("----your email id----");

27

28

            //Locate the Start Free Testing button and click it

29

            driver.findElement(By.xpath("//*[@id='testing_form']//button")).click();

30

    }

31

32

33

    @AfterClass

34

    public void burnDown() {

35

            driver.quit();

36

    }

37

38

}

Let us first understand the code above and then see the execution part.

  • The code under the **@BeforeClass** method: The method setUp() contains the generic code to instantiate the ChromeDriver and navigate to the LambdaTest website. The page is then maximized.
  • The code under the **@AfterClass** method: Similar to the setUp method, the burnDown() method simply closes the browser instance.
  • The code under the main **@Test** method: This is the method consisting of our test case. We simply locate the text field where we have to enter the email ID and then pass on the string (which is our email ID) to the sendKeys() method. Next, we locate the ‘Start Free Testing’ button and click the same.

Note: The code to use the Selenium sendKeys() method can also be written as shown below:

Java

1

WebElement emailField = driver.findElement(by.id(“useremail”));

2

emailField.sendKeys(“---your email id---”);

This is just the bifurcation of the web element and then passing the string value to the text box.

On running the above test, you will see that the browser actions are performed, and your email is entered into the email ID text field.

Did you see how easy the Selenium sendKeys() method is?

Using Selenium sendKeys To Erase Text

Next, let us consider a scenario where we need to verify that we are able to clear or delete the string entered in the text field. To achieve this case, we will use the Selenium clear() method, which simply erases whatever text is entered in the text field.

Consider the below code for the same:

Java

1

package sendkeys;

2

import org.openqa.selenium.By;

3

import org.openqa.selenium.WebDriver;

4

import org.openqa.selenium.chrome.ChromeDriver;

5

import org.testng.annotations.AfterClass;

6

import org.testng.annotations.BeforeClass;

7

import org.testng.annotations.Test;

8

9

public class SendkeysTest {

10

11

WebDriver driver;

12

13

    //Method to set up the browser and open the dummy website

14

    @BeforeClass

15

    public void setUp() {

16

17

            System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\drivers\\chromedriver.exe");

18

            driver = new ChromeDriver();

19

            driver.get("https://www.lambdatest.com");

20

            driver.manage().window().maximize();

21

    }

22

23

    @Test

24

    public void testSendkeys() {

25

            //Locate the email and use sendkeys to pass the email to the field

26

            WebElement email = driver.findElement(By.id("useremail"));

27

            email.sendKeys("---your email id---");

28

29

            // Erase the text entered in the email id field

30

            email.clear();

31

    }

32

33

34

    @AfterClass

35

    public void burnDown() {

36

            driver.quit();

37

    }

38

39

}

The code is the same as we explained earlier, but the only addition is the clear() method used for the email web element. This clear method would erase the text entered in the text field, and you can further enter the text using the sendKeys() method as per your requirement.

Now that we have seen how to sendKeys() in Selenium with its implementation, we hope you understand it better. We have also seen how we can erase the string or text already entered in the text field using the clear() method. We will now explore some other options to enter text in the text field without using sendKeys().

open source tutorial selenium tutorial

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

An Open-Source Book About the Open Source World

Open source today is a word that often include a lot of things, such as open knowledge (Wikimedia projects), open hardware (Arduino, Raspberry Pi), open formats (ODT/ODS/ODP) and so on.

Top 8 Java Open Source Projects You Should Get Your Hands-on [2021]

Top 8 Java Open Source Projects You Should Get Your Hands-on [2021]. Learning about Java is no easy feat. It’s a prevalent and in-demand programming language with applications in numerous sectors. We all know that if you want to learn a new skill, the best way to do so is through using it. That’s why we recommend working on projects.  So if you’re a Java student, then […]

Selenium Training | Selenium Tutorial | Selenium Course | Intellipaat

🔥Intellipaat Selenium training : https://intellipaat.com/selenium-training/ In this selenium tutorial you will learn end to end about selenium course. So in ...

Did Google Open Sourcing Kubernetes Backfired?

With Google not owning the trademarks or control for Kubernetes, it also provided a competitive edge to AWS, Microsoft, IBM etc.

WebDriverIO Tutorial: Handling Alerts and Overlay In Selenium

In this WebDriverIO tutorial on alert handling in Selenium, I’ll show you how to handle alerts and pop-ups as well as overlay modal in WebDriverIO.