SendKeys in Selenium WebDriver With Example

In this tutorial, I will show you how sendkeys in Selenium Webdriver to emulate keyboard key press actions.In our previous articles, we have already seen how to set desired value in a textbox using the “sendKeys()” method of WebDriver’s WebElement class.

SendKeys in Selenium Webdriver With Example

This article especially focuses on emulating non-text keyboard events using Actions class because it is more reliable than WebDriver’s WebElement class for emulating keyboard events.

SendKeys in Selenium Webdriver

Why Emulating Keyboard Events is Required in Selenium WebDriver?

The following are a few of the examples wherein we need to emulate keyboard press (non-text keys) actions like a real user.

  • Copy and Paste: Copying all existing text from any TextBox/ TextArea to another TextBox/ TextArea  by pressing CTRL+AandCTRL+Cfollowed byCTRL+V.
  • Typing Camel Case letters: If you need to type either capital letters or camel case letters like (GoodDay).In such cases, you can press the “SHIFT” key and then type the required characters in the input text field, the letters will be typed in capital letters until the “SHIFT” key is pressed.
  • Pressing Tab: Pressing the “Tab” key.

These are a few examples however, there could be many more as per the test scenario of the application.

Example of Doing Copy and Paste Text from one TextBox to another TextBox

Test Scenario

  • Launch the Chrome browser
  • Open the page
  • Enter the First Name in the “First name:” text box
  • Select the value of the first name by pressing CTRL+A
  • Copy the value of the first name by pressing CTRL+C
  • Press the “Tab” key to set the focus on the Last Name
  • Select the existing value of the last name by pressing CTRL+A
  • Paste the first name value into the Last Name text box by pressing CTRL+V
  • Build and perform the above series of actions

Example of Typing Camel Case Text in a TextBox

Test Scenario

  • Launch the Chrome browser
  • Open the page
  • Enter the came case text value “ABcdEF” in the “First name:” text box
  • Move the mouse over “First name:” and click on it using Actions class to set focus on it
  • Type text “ab” by pressing the “SHIFT” key.
  • Release the “SHIFT” key and type “cd”
  • Again type text “ef” by pressing the “SHIFT” key
  • Release the “SHIFT” key and build and perform the series of actions


We learned how to use Actions Class to Send Keyboard Keys in Selenium Webdriver. You can have a look at the above examples and try to understand them. It will help you out to perform any keyboard event action in Selenium.If you find this article helpful please don’t forget to share it.

Note: After performing any keyDown(theKey) method you must call the keyUp(theKey) or sendKeys(Keys.NULL) to release the pressed the key otherwise it would remain in the pressed state.

Recommended Posts

Leave a Reply