Test Double Click on Real Devices & Browsers

Test Double Click in Selenium to handle elements in real user conditions on real device cloud

Get Started free
Home Guide How to perform Double Click in Selenium?

How to perform Double Click in Selenium?

By Jash Unadkat, Community Contributor -

The double-click action, whether on a mouse on a trackpad, is one of the most common user actions on a computer. This also applies to web surfing. A website may contain multiple elements requiring a double click to launch specific functions.

To ensure these elements perform as expected, QAs must test them thoroughly. QAs primarily use Selenium click commands to simulate a mouse click interactions with a particular web application.

This short article will demonstrate how QAs can automate the double click operation in Selenium (with Java) using the Actions class.

Importance of Selenium Click Command

The ‘click’ operation is one of the primary tasks of any computer. You must click everywhere, from opening a file to accessing the internet, navigating to a web page, and interacting with web elements (links, buttons, search bars, etc.). Similarly, double click and right click are standard operations for your computer daily.

Selenium click() command emulates the click operation on elements like buttons, links, etc. By using it, testers can save a lot of time spent on manual testing and identify bugs.

Prerequisites: To perform a specific operation on a particular element, users are expected to understand locators in Selenium and Actions class before starting with this demonstration.

How to Perform Double Click in Selenium?

As with any automation process, the first step is to navigate to a particular web page and locate a specific element on which the double click action needs to be performed.

Once the element is located, perform the Double Click operation using the Actions class.

Sample code snippet:

driver.get("URL of target website or webpage"); // Define the URL of the target website.
Actions act = new Actions(driver);

//Double click on element
WebElement ele = driver.findElement(By.xpath("XPath of the element")); 
act.doubleClick(ele).perform();

The code above will do the following:

  1. Navigate to the desired website on which the test needs to be performed.
  2. Instantiate the Actions class and locate the target element.
  3. Perform the Double Click operation on the located element.

One must also know: Effective ways to use XPath in Selenium

Refer to the complete code below that illustrates the Double Click operation, automated via Selenium and Java:

Note: One needs to define the driver path, the URL to be tested, and the locator strategy in the code below.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DoubleClick{

public static void main(String[] args) {

System.setProperty("<path of the chrome driver>");

WebDriver driver = new ChromeDriver();
driver.get("URL of target website / webpage") Define the URL of the target website.

driver.manage().window().maximize();
//Instantiating Actions class
Actions act = new Actions(driver);

//Locate WebElement to perform double click 
btnElement = driver.findElement(<locator of the element>);

//Double Click the button
act.doubleClick(btnElement).perform();

System.out.println("Double click operation performed");

driver.quit();

}
}

BrowserStack Automate Banner 7

How to right click in Selenium?

To automate right click in Selenium, a reliable method – contextClick() can be used. This accepts the target WebElement as the argument. To use this method, use the Actions class object. The process of locating the desired element remains the same. Refer to the code example 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();

}

}

Note that the context_click() method can also take an offset argument to specify the location within the element where you want to right-click

QAs prefer BrowserStack’s real device cloud for running Selenium tests on real devices. It enables them to run Selenium tests in real user conditions across 3000+ real devices and browsers.

The short demonstration above will help QAs simulate the Double Click operation using Selenium WebDriver. Automating this action will also ensure that the web element functions as expected when double-clicked.

Talk to an Expert

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to Double Click on an Element in Selenium Python?

Selenium Click Command (Right click, Left Click, and Double Click)

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers