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.
Join the DZone community and get the full member experience.Join For Free
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?
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.
Let us first understand the code above and then see the execution part.
- The code under the
@BeforeClassmethod: 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
@AfterClassmethod: Similar to the setUp method, the
burnDown()method simply closes the browser instance.
- The code under the main
@Testmethod: 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:
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:
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
How Can We Type In Selenium Without Using sendKeys?
sendKeys() works almost every time, there might be a case where the text input box is disabled, and you need to pass some text to the field. In such scenarios,
sendKeys() would not work, and you might have to try some alternate approach. Before we move onto the use case, remember that this is not the recommended way to test as the UI blocks you from entering any text in a field. Just for the sake of understanding our use case, let us consider such a scenario.
sendKeys(). Try using these code lines in our example above by replacing the
sendKeys() code and seeing that you will get the same results as with the
Now that you understand how to
sendKeys() in Selenium let us now see how the LambdaTest Selenium Grid cloud can efficiently execute your tests across different system configurations.
Implementing Selenium sendKeys On LambdaTest Grid
It is highly likely that you have to run your automation suite across multiple browsers and OS configurations. LambdaTest Grid provides you an easy solution to execute your tests across multiple combinations by leveraging capabilities. These capabilities can be generated directly from the LambdaTest capabilities generator and integrated into your Selenium test.
Note that we will use the same example used above for execution on LambdaTest Grid cloud for one browser, but you can use the same test to perform cross-browser testing by changing the capabilities and using Data Provider, as explained in our article on parallel testing.
You may change the capabilities settings as per your requirements and execute the test. You will see that the LambdaTest platform provides you a beautiful user interface of the test results, including a video of the execution. You can find these results under the Automation Logs tab in the Automation dashboard as shown below-
See how seamless Selenium automation can be by leveraging the capabilities of LambdaTest Grid cloud? Now, go ahead and try running a test case of your own that uses the
sendKeys() method and leverages the capabilities of LambdaTest.
- We understood what the Selenium
sendKeys()method is and how it can be used in Selenium automation.
- We saw how the
clear()method could be used to clear off any string that is already present or entered in a text field.
- We then saw how we could use the
I hope you found this article informative; I would love to hear what you think. Don’t forget to leave your comments below.
Published at DZone with permission of Praveen Mishra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.