Understanding Click Command in Selenium

Jash Unadkat
3 min readSep 24, 2020

--

This article aims to explain how QA engineers can mimic the mouse operations using the Click command in Selenium

A click is the most fundamental user action performed by anyone accessing the internet. It allows the user to navigate web pages or perform particular tasks by interacting with links, buttons, and other web elements.

Every web page comprises of several web-elements (Links, buttons, radio buttons, Checkboxes), and every website has multiple web pages

For interacting with web elements in test automation, QA engineers use the Click command offered by Selenium WebDriver. It helps simulate the click action users perform in the real world.

For automating advanced click operations like double clicks, hovering, drag and drop, QA engineers use the Action Class

How to perform a Left Click in Selenium?

A left-click is a fundamental operation. To perform a left mouse click, one needs to locate a particular element first and then perform the click operation using the click() command.

Let’s consider a sample scenario: Visit BrowserStack.com and click on the Get Started free button.

Code:

driver.get("https://www.browserstack.com/");
driver.findElement(By.id("signupModalButton")).click(); //using Selenium click button method

The code above when executed, Navigates to the BrowserStack website and locates the “Get Started Free” button using its “id” as the locator and performs a left click on it

How to perform a Right Click in Selenium?

For automating the right-click operation, Selenium provides a dedicated method — contextClick(). This method accepts the target WebElement as the argument. In order to use this method, use the Actions class object. The method of locating the desired element remains the same.

Refer to the code below:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickDemo {
public static void main(String[] args) {//Set system properties
System.setProperty(" < Path of the Browser Driver > ");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// Visiting the URL
driver.get("<Enter the target URL>");
//Maximise browser window
driver.manage().window().maximize();
//Instantiating the Actions Class
Actions actions = new Actions(driver);
// Fetching or locating WebElement to perform right-click using the desired locators
WebElement btnElement = driver.findElement(By.id("id-value"));
//Right click the button to display Context Menu
actions.contextClick(btnElement).perform();
System.out.println("Context Menu displayed");
// Code To click on a specific option from the Context menu once right-click is performed.
WebElement elementOpen = driver.findElement(By.xpath("<Xpath of the specific option"));
elementOpen.click();
// Accept the Alert
driver.switchTo().alert().accept();
System.out.println("Right click Alert Accepted Successfully ");// Terminating the operation
driver.close();
}}

The code above, when executed, navigates to the target website, locates the particular web element, performs the right-click operation, and selects the desired option from the context menu.

Performing a Double Click in Selenium

In some cases, a user needs to double click on a particular button and open a folder or file while performing browser testing. Similar to the right-click operation, the Actions class can be used to simulate the double click. Refer to the Syntactic Code below that explains how to perform the double click operation in Selenium:

driver.get("URL of target website / webpage"); // Define the URL of the target website.
Actions act = new Actions(driver);
//Double click on element
WebElement web2 = driver.findElement(By.xpath("XPath of the element"));
act.doubleClick(web2).perform();

One can refer to this detailed tutorial on drag and drop in Selenium to understand how to automate drag and drop operation while Selenium testing.

Click operations are fundamental yet vital for test automation. Given this, QAs must be well aware of using the above commands.

--

--

Jash Unadkat
Jash Unadkat

Written by Jash Unadkat

As a tech geek, I love writing articles about everything related to web development or software testing space.

No responses yet