How to Set Text Value in TextBox In Selenium WebDriver

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.enter or set text value in a text box in Selenium WebDriver

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.

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.

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.

set a text value in a textbox in Selenium WebDriver

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.

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.

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.

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.

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.

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.

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

Leave a Reply