Selenium Tutorial for Beginners

How to enter value in ‘Textbox’ using Selenium WebDriver

Now that we are aware of the most common web elements and the corresponding User actions to be performed, let’s get started with learning the basics. The first element we picked is the most common – “Textbox” and the corresponding User action – “Enter text”. Let’s see how to identify the Google Search textbox & enter the Search string…

Inspect the element to be identified

This can be done by using browser tools. In simple terms just right click on the element & select ‘Inspect Element’.

Browser Inspect Element

Look for certain ‘attribute-value’ pairs

In the highlighted HTML, look for certain ‘attribute-value’ pairs such as class=”gsfi” OR id=”lst-ib” OR name=”q” OR title=”Search”. Using this “attribute-value” pairs Selenium WebDriver identifies the web elements.

<< THE CODE >>

a. Open a browser

b. Open the website URL & maximize the window

c. Find the web element to perform action on. (in this case a textbox)

d. Perform the action. (in this case ‘enter text’)

Open Browser & Application

The first four lines of code System.setProperty to driver.manage() opens a Chrome browser >> opens the URL >> maximizes the browser window. For details please visit our earlier post – #2. Selenium Automation Testing | How to Launch a Browser.

Identify the Textbox

These are the different mechanism to identify the same textbox – by using different attributes such as id, name, className or xpath. Here we have kept just one active & commented the others. For details on different element locators, click here…

After identifying the textbox, we are saving it in the ‘Searchbox’ object of type ‘WebElement’ which is an in-built class available in Selenium. We need to import this class in order to create objects,

Enter the Text

‘sendKeys’ is a pre-defined function of the WebElement class which enables Selenium WebDriver to enter the text. As the name suggests you can think of it as imitating the keyboard input.

The Result

Once you run this program (a test case) it will open the browser >> open the Google URL >> Enter the search string in the textbox.

Selenium Enter Text sendKeys

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Leave a Reply

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