The Issue

During automation, there will be instances when you may need to perform an action that a simple click command won’t satisfy. Pressing Enter, hitting Tab, deleting text, or using some combination of keyboard shortcuts would all fall into this category.

The Answer

Selenium Webdriver provides a send_keys method which enables the use of keyboard keys or shortcuts while automating test cases. These will perform the specified action after running the command, allowing us to successfully execute a test.

The Code

Below is an example exhibiting the usage of keyboard keys:

In the above example, “:enter” is used instead of clicking on the Next button. By using the “:enter” key, it goes to the next page where the password has to be entered. After the password is entered, “:enter” is again used to sign in to the Gmail account instead of clicking on the “Sign In” button, which does the same operation.

The Result

The above code can be saved as a ruby (.rb) file and yields the following result

  • Opens the browser
  • Navigates to “Google Sign In” page
  • Enters the username and hits enter
  • Enters the password and hits enter
  • Signs into your Gmail account
  • Close the browser

The Takeaway

By using Selenium to send our keyboard keys, we have yet another tool employ as we write our test automation scripts. You can get creative and substitute click commands with key sends, or use the key send to resolve a specific issue that your click commands could not. There are numerous other Selenium key commands beyond the essential ones we’ve described here, experiment and find which key commands work best for your tests!