How to handle text box in selenium

handling text box

How to handle text box in selenium

In this tutorial, we will see how to enter text in the text field in the selenium web driver.

When you try to work on a text field or label where you need to enter some data.

Let’s understand how text box / Field looks like in HTML. Check the below snapshot.

text box html

When the Html page is designed with type as “text” then the text field will be created it will be reflected and the user can able to enter data into it.

text box

In selenium web driver you can handle text field using the SendKeys method.

We will see How to handle text box in selenium step by step,

Step 1: Inspect the element and find the unique element (Xpath).

Step 2: Find the element and use the SendKeys method.

Step 3: Validate whether you have entered data or not using automation.

Step 1: Inspect the element and find the unique element (Xpath).

Xpath: //input[@id=’firstname’]

After finding Xpath you need to make sure 3 things whether you have written valid Xpath, Element is highlighted and having unique (1 of 1 node in Html). Please look below screenshot.

text box xpath

Step 2: Find the element and use the SendKeys method.

In the above expression, sendKeys is a method to handle text using selenium webdriver.

Step 3: Validate whether you have entered or not using automation.

This is the main thing we need to consider for every interview or real-time as we need to cross-check whether we have entered or not? In Selenium Webdriver there are many ways we can validate but best one is retrieving the data from it

Validating Methods:

getAttribute(“value”)

The above method will return a string and it will return the data. We can store that data in a variable and can validate the same.

Please check below code snippet for validation part:

Based on contains method we can check whether a text is present or not. Please practice once on this automation practice page where you have a text field.

Please check entire program on how to handle text field in selenium webdriver below,

Output:

text output

Note: To run the above program you need to download browser driver as I have downloaded chrome. You can download and work based on your desired web browser driver or you can click here to download from the selenium website.

You May Also Like

About the Author: Azgar Khan

Leave a Reply

Your email address will not be published. Required fields are marked *