How to click a button in Selenium Webdriver using Java
Selenium Tutorial for Beginners

How to click a Button in Selenium WebDriver using Java

Continuing on our Selenium Beginner’s series, let’s see how to Click a Button in Selenium WebDriver using Java. Hope you have already gone through the previous tutorials which will be extended here…

#4. Common Page elements & User actions to automate

#5. How to enter value in ‘Textbox’ using Selenium WebDriver

Here we will try to login to Gmail by just entering the Email ID and then clicking on the ‘Next‘ button.

Click a ‘Button’ in Selenium WebDriver using Java

Inspect the Element to be clicked

This can be done by using browser tools. In simple terms just right click on the element (Email ID textbox and then the ‘Next’ button) & select ‘Inspect Element’.

 

How to click on a button using Selenium WebDriver

<< THE CODE >>

The steps followed are,

a. Open a browser

b. Open the website URL & maximize the window

c. Find the Email textbox and enter the Email ID (for reference, click here…)

d. Identify the ‘Next’ button and ‘Click’ on it.

Open Browser & Application, Enter the Email ID in textbox

The code till,

is to open the browser and application >> Identify the Email textbox and enter the Email ID. For reference, please visit our earlier posts in the series.

Identify the Button to be clicked

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

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

Click a Button in Selenium WebDriver

‘click’ is a pre-defined function of the WebElement class which enables Selenium WebDriver to click on any web element. As the name suggests you can think of it as imitating the mouse click event. The commented code shows how to combine the identification & click activities in a single statement (separated by a dot(.) operator).

The Result

Click a Button in Selenium WebDriver | Once you run this program (a test case) it will open the browser >> open the Gmail URL >> Enter the Email ID >> Click on the ‘Next’ button.

Save

Save

Save

Save

Save

Save

Leave a Reply

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