Home Guide How to perform Double Click in Selenium

How to perform Double Click in Selenium

By Jash Unadkat, Technical Content Writer at BrowserStack -

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 comprise multiple elements that require a double click to launch specific functions.

In order to ensure that these elements perform as expected, QAs need to test them thoroughly. QAs primarily use Selenium click commands to simulate 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.

Prerequisites: In order to perform a specific operation on a particular element users are expected to have a fundamental understanding of locators in Selenium and Actions class prior to 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();

}
}

Run Selenium Tests on Cloud for Free


Read More: How to Drag and Drop in Selenium


QAs can 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 2000+ 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.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to handle Action class in Selenium

How to perform Mouse Hover Action in Selenium

What is Test Monitoring and Test Control?

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free