Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

Test Automation With Selenium Click Button Method(Examples)

Aditya Dwivedi

Posted On: November 25, 2019

view count274416 Views

Read time12 Min Read

One of the most fundamental and crucial interactions while Selenium automation testing is done by automating click operations over elements on a web page. We facilitate the click interaction using a method called

Selenium click button method, although, is one of the most basic drills, it is often used inefficiently. Even experienced tester seems to miss out on the entire implementation of the click method is Selenium. They become occupied with writing complex test scenarios and forget that sometimes, a random click in the UI of a web application, can lead to a bug. This is why, today, I will be talking about different ways in which you can make use of command to help you perform test automation in a faster and better manner.

In this article, I will cover in great detail. We will automate from basic to advanced interactions through the click.

If you’re new to Selenium and wondering what it is then we recommend checking out our guide – What is Selenium?

If you are an experienced automation tester, you can skip the basic operations and head out to the advanced mouse operations. For beginners, we will start with a basic introduction of Selenium testing. Without further ado, let’s go!

What Is Selenium?

Selenium is one of the most popular automation testing frameworks. Being a free, and open-source framework has made it a popular choice among the global tester community for automating the web testing experience. Selenium automation testing offers support for multiple programming languages and browsers like Google Chrome, Mozilla Firefox, Safari, Microsoft Edge, Internet Explorer, etc. making it a perfect choice to perform automated cross browser testing. Its popularity as an open-source has helped it to deliver compatibility across numerous test automation frameworks too! Let us have a quick and basic run down.

Selenium has 4 variants:

  1. Selenium IDE(Integrated Development Environment) – It is a framework which is used to automate web application only. it is a record and playback combination tool which is used to automate web app in Firefox and Chrome browser.
  2. Selenium RC(Remote Control) – Selenium Remote control is also called selenium 1.0. It supports all browsers but we need to start the server first if we use selenium RC. its execution is slower. It does not support HTTP protocol.
  3. Selenium WebDriver – Selenium webdriver is a webpage automation tool.while is used to automation web page only. There isn’t any need for a server to work with Selenium WebDriver. Its directly connect to browsers using driver.
  4. Selenium Grid – Selenium Grid is a combination of webdriver client library and RC(Remote Control) library . we use selenium Grid to execute the program in the remote control.

Testing with Selenium

Selenium Webdriver also supports a headless browser like PhantomJS. Meanwhile, Selenium WebDriver has some disadvantages too like WebDriver cannot automate captcha. Using Selenium WebDriver for gaming and audio & video testing is not possible. The biggest flaw for Selenium WebDriver lies with its manner of test execution. Selenium WebDriver allows serial execution of test cases which makes it a little troubling for complex and large web-applications or test suite as it can take quite a while to validate sufficient test coverage.

As a solution, the Selenium Grid was introduced to allow testers to perform parallel testing with Selenium. Today, where automation testing is on the rise, Selenium plays a crucial role in helping startups & large enterprises into-market launch.

For a more detailed difference between these 4 Selenium variants, refer to our Selenium WebDriver tutorial.

What Is Selenium ‘.click()’?

Selenium offers a ‘.click()’ method to help you perform various mouse-based operations for your web-application. You can use the Selenium click button method for various purposes such as selecting the radio button and checkbox or simply clicking on any button or link, drag and drop, click and hold, etc.

In the next section, I am going to demonstrate the practical implementation of Selenium click button method in for basic operations, and advanced operations. After that, I will also show you how you can use CSS selectors with the Selenium .click method.

Basic Operations Using Selenium.Click()

Let’s start with the basic operations that your perform using the Selenium click button method for automation testing. We will do so with the help of Action class. Action class is an ability used to handle any mouse operation with the Selenium click button method. It comes as an in-built ability for Selenium automation testing. You can also leverage action class for keyboard interactions.

Performing Mouse Click/Left Click In Selenium Testing

The most basic operation using a Selenium click button method is a left-click or a mouse click.

Test Scenario: Visit LambdaTest Homepage and click on the Login button.

Test Automation With Selenium

Example code for the test scenario:

You can also use the Selenium .click() button for enabling/disabling checkboxes, radio buttons.

Test Scenario 2: After you click on the LambdaTest Login button, tick the checkbox for “Remember me”.


Note: To confirm whether a checkbox or a radio button is selected or not, we make us of the isSelected().

This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.

Here’s a short glimpse of the Selenium 101 certification from LambdaTest:

Performing Right Click In Selenium Testing

In some of the scenarios while Selenium automation testing of a web page, you may need to automate right click over a button, text, image or more.

Test Scenario: Go to LambdaTest Live and open the video in a new tab.


Action class may be ineffective to automate this scenario as we are trying to open a right-click menu of the Google Chrome browser. Action class has few restrictions around the keyboard usage, you can only declare modifier keys such as Shift, Ctrl, CapsLk, etc. using the actions class. However, you can use the Robot class to leverage keyboard inputs that are different from modifier keys.

We will be using the Robot class here to capture the elements of the right-click menu and perform a click on one of those elements to automate using the Selenium click button method.

Performing Mouse Hover In Selenium Testing

Mouse hover is a vital part of any cross browser testing checklist. Many times you want to check whether the styling applied over a web element is supported in a consistent manner when accessed from different browsers or not? You can also use a mouse hover to verify a prompt message over a button.

Test Scenario: Visit LambdaTest Pricing and perform a mouse hover on the text “What are parallel tests?”. After understanding what parallel tests are picking a parallel test value from the drop-down.


In a few projects, some time elements are not visible or element is disabled in this case we need to take the help of JavaScript Executor. While working on not visible elements gives an “illegalStateException” and we can get rid of this using the JavascriptExecutor.

Example Script for Test Scenario:

Advanced Operations Using Selenium.Click()

With basic operations covered, we now step into performing advanced operations using the Selenium click button method.


Using Selenium Click Button with X,Y coordinate

We can use movebyOffset to perform a click anywhere on the page by feeding (x,y) coordinates to the WebDriver. You can even generate a click on blank portions of your webpage as long as you are aware of the (X,Y) coordinates.

Test Scenario: Visit LambdaTest Homepage and find the (x,y) coordinates for the button “Start Free Testing”.

Selenium Click Button

In the above image, you can notice the X, Y coordinates specified under the highlighted “Start Free Testing” button.

Note: These (X, Y) coordinates will be different from one screen resolution and screen size to another.

Performing Double Click In Selenium Testing

Double click is an absolute must for Selenium automation testing be it for performance or automated browser testing. Here is an example of how you can perform double click using Selenium automation scripts.

Click And Hold Using Selenium Automation Testing

Many times when you are performing automation testing with Selenium, you may come across a requirement where you would need to highlight a text, image by clicking and holding the mouse. Let us take a scenario to automate this interaction.

Test Scenario: Visit JQuery UI Draggable Interface, perform a click and hold over the draggable box and then move using the offset.

Selenium Automation Testing

Here is the code to automate click and hold, along with movebyoffset.

Performing Drag and Drop In Selenium Testing

Somewhat similar but slightly different than Click and Hold, is the requirement for Drag and Drop. Here, the place to drop after dragging is specifically prominent in the UI so you know where to place an element.

Test Scenario: Visit JQuery UI Droppable Interface, perform a click on the draggable box, and then drop it in the container box.


Here is how you can automate the above-mentioned test scenario using the Selenium click button method.

Locators & Selenium Click

Selenium locators allow you to locate a web element over a web page. There are 8 types of locators(identifire) in Selenium. Let’s see how you can perform a click in Selenium using different Locators.

1. ID()

Wherever an ID is specified in the HTML code for any web element, you can leverage that in your Selenium automation testing scripts to locate that particular web element. The ID of a web element would always be unique which makes it a very powerful way to locate web elements automatically using Selenium automation testing. To automate clicks using an ID locator you would need to specify in your code as below.

Making The Move With ID Locator In Selenium WebDriver

2. Name()

Similarly, you can find the name of a web element by inspecting the HTML and use it to locate the web element using Selenium automation testing. To use the Selenium click button with Name locator, you need to address it in a similar way.

How To Use Name Locator In Selenium Automation Scripts?

3. Class Name()

I rarely use the Class name as an alternative to other locators in Selenium. Inspect a web element, find its class name, and locate it using your Selenium automation scripts.

Selenium Java Tutorial – Class Name Locator In Selenium

4. Tag Name()

Whenever there is a webelement with unique HTML tag we can use the TagName locator in Selenium automation testing. There can be many types of Unique HTML tag such as:
Hyperlink – < a >
Image – < img >
Header – < h1 >

So for example, if you have a unique Hyperlink TagName using the anchor tag then you can incorporate Selenium click button interaction using the below code.

Locating Elements by TagName In Selenium

5. Link Text()

If there is a hyperlink with a unique HTML tag then we can make use of the LinkText locator. Here we specify the text over which a Hyperlink is triggered on a webpage through a click.

6. Partial Link Text()

You can specify the text for a hyperlink even partially and locate it using the Partial Link Text locator. For example, if we wish to locate a web element with a hyperlink that says Amazon then we can do so using the below command.

Find Elements With Link Text & Partial Link Text In Selenium

7. CSS Selector()

A more cross browser compatible locator than Xpath is a CSS selector as a primary browser such as Google Chrome and Mozilla Firefox are designed to optimize the execution time for a CSS selector.

Note: Whenever we use CSS selector or Xpath as a locator we should not use mandatory attribute because mandatory attribute will be the same for similar kind of webelement so it is always recommended to go for additional attributes.

How Pro-Testers Use CSS Selectors In Selenium Automation Scripts?

8. Xpath()

XPath is a locator that is used to locate the webelement based on the tag, attribute, text, etc. we can use Xpath for HTML as well as for XML Docs.

Complete Guide For Using XPath In Selenium With Examples

Bonus: Random Links Using Click Method In Selenium

Here is a cool tip using which you can click on every hyperlink on a webpage. Gotta click em all!

Test Scenario: Visit LambdaTest Homepage and click on every hyperlink invoked through the anchor tag in HTML.

Below is a script which is generating a list of hyperlinks on the LambdaTest Homepage where the tagName is “a” which represents an anchor tag. After that, it would click on every link a single time, in random order. The script would visit the first link, go to the respective web page and then will roll back to LambdaTest to visit the next link and the cycle will go on till every link is visited a single time.

Console Output:


As you can see there were in total 47 hyperlinks that were accumulated by the test automation scripts. They are visited one by one! I will leave it for you to find a way of incorporating this in your automated browser testing scripts.

Bonus: Java Loop Using Selenium Click Button Method

Sometimes to reproduce a bug on a UI element, you may need to automate a loop to witness whether a bug was encountered in X number of trials. I am going to show you how to create a Java loop for the Selenium click button.

Test Scenario: Login to the LambdaTest application. Go to the profile icon on top-right and click to open the menu, then click to close it. Continue the drill for 75 clicks to see if any anomaly is encountered.

Here is a recording of the test execution:

Did We Click Some New Ideas?

By far, I hope you have a good understanding of how powerful the Selenium .click() button method can be and how you can leverage it. I hope the bonus tips were helpful for your daily automated browser testing scripts. Do you want to share a bonus tip too or a unique way in which you used the Selenium click button method? If so, drop your thoughts in the comment section below! Happy testing! 🙂

Subscribe to Coding Jag and get the best news around the testing world delivered to your inbox every Thursday morning.

Author Profile Author Profile Author Profile

Author’s Profile

Aditya Dwivedi

Aditya is a test automation enthusiast at LambdaTest who is always fascinated about new testing frameworks and programming languages.

Blogs: 9


Test your websites, web-apps or mobile apps seamlessly with LambdaTest.

  • Selenium, Cypress, Playwright & Puppeteer Testing
  • Real Devices Cloud
  • Native App Testing
  • Appium Testing
  • Live Interactive Testing
  • Smart Visual UI Testing
Book a Demo