A textbox is a very commonly used element to take input from a user. I am going to show you how to set the desired value in a textbox in Selenium WebDriver.We will also see how to get the value of a text in the textbox along with other validations like whether the textbox is Enabled and is Displayed on the page and many more.
We will consider the following DOM structure for the examples.
TextBox Automation in Selenium WebDriver
Enter or Set a text in a TextBox In Selenium WebDriver By ID
We can enter a text value in a textbox using the following code snippet example.
1 2 3 |
driver.findElement(By.id("fname")).sendKeys("Micheal"); |
Set Text in a Textbox Using CSS Selector ID attribute
The following example shows how to set text values in the First Name textbox using the CSS Selector ID attribute. To more about CSS Selector locator please refer to my post How to Use CSS Selector in Selenium WebDriver | 13 Examples.
1 2 3 4 5 |
//Set text in the First Name TextBox using CSS Selector ID attribute driver.findElement(By.cssSelector("input[id='fname']")).sendKeys("William"); |
Note: If any textbox has a default value set on it and you will just set a value on it using Selenium WebDriver, Selenium does set the value after the default value. For example, as per the above screenshot, the first name has a default value John and if we set a new first name value William using Selenium WebDriver the output would be as follows. The value in the textbox will be JohnWilliam rather than just William. So it is the best practice to first clear out the value of a textbox before setting a new value. We will see it in the next example.
Enter Text in a Textbox after Clearing its value Using CSS Selector Class attribute
The following code snippet enters text in the Last Name text box using the CSS Selector ID attribute. I this example we will first clear the default value of the last name and then set a new value to it.
1 2 3 4 5 6 |
//Set value of last name after clearing default value of last name textbox driver.findElement(By.cssSelector("input.txt-bc")).clear(); driver.findElement(By.cssSelector("input.txt-bc")).sendKeys("Brown"); |
Set Value in a Textbox Using XPath In Selenium WebDriver
The following Selenium code sets the value in the First Name Textbox using the XPath by its ID property. To know more about XPath please refer to my post XPath in Selenium WebDriver with Examples.
1 2 3 4 5 6 |
//Set value in the First Name TextBox using XPath driver.findElement(By.xpath("//input[@id='fname']")).clear(); driver.findElement(By.xpath("//input[@id='fname']")).sendKeys("Wesley"); |
Enter Text in a TextBox Using XPath When no Unique property is found
In the above examples, we are easily able to enter text in any of the TextBoxes using XPath, ID, and CSS Selector by using the ID property of the Textboxes as each Textbox has its unique ID property. But in real projects, you might come across scenarios wherein it becomes very difficult or next to impossible to find a property unique property like ID or name and etc for a web element. If you look at the HTML Tags of the Textboxes you will observe that all the textboxes are having the same name property “name”.If we try to select the Last Name Textbox using its name property it won’t work. Selenium Webdriver will always set the value in the First Name Textbox as it comes first in the DOM structure. So in the following example, I will show you how to locate the second Textbox that is the “Last Name” using XPath by its name which is not a unique property.
1 2 3 4 5 |
//Set test in Last Name TextBox using XPath by its name that is not a unique property driver.findElement(By.xpath("//input[@name='name'][2]")).sendKeys("Wesley"); |
How to check whether a Textbox is Visible and Enabled on the Page?
Whenever you are going to perform any action on a web element it is standard practice to first check whether it is visible on the page. If it is visible the next thing we should check whether it is enabled or not. The following example illustrates how to do it. Here we will first check whether First Name TextBox is visible on the page using isDisplayed() method. If it is visible we will check whether it is in the enabled state using the isEnabled() method. If it is enabled we will enter the text in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * Validate whether the TextBox is visible and enabled */ Boolean visibleState =driver.findElement(By.id("fname")).isDisplayed(); System.out.println("The Textbox visibility is: "+visibleState); if(visibleState) { Boolean status =driver.findElement(By.id("fname")).isEnabled(); System.out.println("The status of Textbox is: "+status); //If TextBox is enabled set a value to it if(status) { driver.findElement(By.id("fname")).sendKeys("Bob"); } } |
How to Get attribute ( maxlength ) of a Textbox using getAttribute?
The getAttribute method is quite helpful to validate the value of any of the property of a web element. Consider the following HTML Tag.
1 2 3 |
<input type="text" id="username" name="username" maxlength="10"> |
As per the HTML Tag, the username text box can accept only ten characters.
If we have to get the value of maxlength of the textbox we can use the following Selenium Code snippet.
1 2 3 4 5 6 |
/** * Get the textkbox attribute value */ String iMaxLength=driver.findElement(By.id("fname")).getAttribute("maxlength"); System.out.println("value of maxlength is: "+ iMaxLength); |
Conclusion
- We can set value in Textbox by using the sendKeys() method.
- We can locate the Textbox elements by id, name, CSS, and XPath selector.
- We can perform validation operations like isDisplayed(), isSelected(), and isEnabled() before selecting the Textbox to get rid of runtime exceptions thrown by Selenium WebDriver.
Recommended Posts
- 23 Most Useful Selenium WebDriver Commands That You Must know
- How to Download and Install Eclipse
- Configure Selenium Webdriver With Eclipse
- How To Install TestNG in Eclipse
- Click a Button in Selenium WebDriver and Carry out Other Validations
- How to Select a Radio Button In Selenium WebDriver And Do Other Validations?
- How to Select a Checkbox In Selenium WebDriver And Do Other Validations?
- Page Object Model (POM) & Page Factory in Selenium WebDriver
- Read and Write Excel File in Java Using Apache POI Lib
- Intuitive Way of MySQL Database Testing in Selenium | LeanFT