
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.

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.

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.

Step 2: Find the element and use the SendKeys method.
1 2 3 4 5 6 |
//Find element and assign in webelement WebElement Firstname=driver.findElement(By.xpath("//input[@id='firstname']")); //Entering the text Firstname.sendKeys("My name is Khan"); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//validating text entered or not? String attribute = Firstname.getAttribute("value"); System.out.println("Entered value:"+attribute); if(attribute.contains("My name is Khan")) { System.out.println("Exact Data Entered"); } else { System.out.println("Data not entered"); } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public class HandlingTextBox { @Test public void HandlingTextField() { //Initialize reference to driver WebDriver driver; //Setting the properties for chromedriver System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe"); //Initialize the chromedriver for webdriver reference driver=new ChromeDriver(); //Launching application driver.get("https://automationexplore.com/selenium-automation-practice-page/"); //Setting implicit wait to load all web elements in application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Maximize the window driver.manage().window().maximize(); //Find element and assign in webelement WebElement Firstname=driver.findElement(By.xpath("//input[@id='firstname']")); //Entering the text Firstname.sendKeys("My name is Khan"); //validating text entered or not? String attribute = Firstname.getAttribute("value"); System.out.println("Entered value:"+attribute); if(attribute.contains("My name is Khan")) { System.out.println("Exact Data Entered"); } else { System.out.println("Data not entered"); } } } |
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.