How to Perform Double Click in Selenium
In this tutorial, we will learn how to perform double click action in Selenium WebDriver. We perform double click action using mouse.
To perform double-click action on the current location of cursor and a WebElement in Selenium, we will use doubleClick() method of actions class.
The general syntax of doubleClick() method to perform the double-click action in Selenium is as follows:
public Actions doubleClick()
This method does not take any input parameters and returns actions class instance. Let’s first perform how to double click action on the current location using doubleClick() method of actions class in Selenium.
Performing Double Click Action on Current Location
Let’s automate a scenario where we will double-click at the current location of the cursor.
Scenario to Automate:
1. Launch the Firefox web browser.
2. Open a URL “https://selenium08.blogspot.com/2019/11/double-click.html”.
3. Locate the element “Double-click” by using By.xpath.
4. Move the mouse cursor from its initial position to the current location.
5. Now, click on the current location (i.e. on Double-click element).
7. Close the web browser.
Let’s see the following Java program code related to the above scenario. Follow all the steps in the below code.
Program code 1:
package seleniumProject;
import java.util.concurrent.TimeUnit;
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 DblClickAtCurrentLocation {
public static void main(String[] args)
{
// Create a driver object for Firfox browser.
WebDriver driver = new FirefoxDriver();
// Maximize the web browser.
driver.manage().window().maximize();
// Store URL in a variable URL of String type.
String url = "https://selenium08.blogspot.com/2019/11/double-click.html";
// Call get() method and pass the URL as an input parameter.
driver.get(url);
// Wait for loading the complete web page.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Locate WebElement by using By.xpath.
WebElement dblclick = driver.findElement(By.xpath("//button[text() = 'Double-Click me to see Alert message']"));
// Create an object of Actions class and pass the driver as an parameter.
Actions actions = new Actions(driver);
// Call moveToElement() method to move the mouse to the location of button element and pass element as an input parameter.
actions.moveToElement(dblclick); // Line 1.
// Call doubleClick() method to perform the double-click action at current location.
actions.doubleClick(); // Line 2.
// Call perform() method to perform a sequence of actions.
actions.perform(); //Line 3.
// The Line 1, Line 2, and Line 3 can also be written in a single line like this:
// actions.moveToElement(dblclick).doubleClick().perform();
System.out.println("Double click action performed successfully at current location");
}
}Output:
Double click action performed successfully at current locationIn the above code, we have used moveToElement(WebElement element) method to move mouse cursor to the location of the button element and performed double-click action at the current location.
The output has been shown in the below screenshot after performing double-click action on the double-click element on the demo web page.
Performing Double Click on WebElement in Selenium
In the previous section, we have performed the double-click action on a current location moving from the initial position. Now, we will perform double-click action on an element using another overloaded version of doubleClick() method.
The general API syntax for doubleClick() method is as follows:
public Actions doubleClick( WebElement onElement)
This method accepts the target element as an input parameter that has to be performed double-clicked.
Here, we will use the same URL as you performed in the previous scenario. Let’s see the following program code for this.
Program code 2:
package seleniumProject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
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 DblClickWebElement {
public static void main(String[] args)
{
// Create a driver object for the Firfox browser.
WebDriver driver = new FirefoxDriver();
// Maximize the web browser.
driver.manage().window().maximize();
// Store URL in a variable URL of string type.
String url = "https://selenium08.blogspot.com/2019/11/double-click.html";
// Call get() method and pass the URL as an input parameter.
driver.get(url);
// Wait for loading the complete web page.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Locate web element by using By.xpath.
WebElement dblclick = driver.findElement(By.xpath("//button[text() = 'Double-Click me to see Alert message']"));
// Create an object of Actions class and pass the driver as a parameter.
Actions actions = new Actions(driver);
// Call doubleClick() method to perform the double-click action at current location.
actions.doubleClick(dblclick); // Line 2.
// Call perform() method to perform a sequence of actions.
actions.perform(); //Line 3.
// The Line 1, Line 2, and Line 3 can also be written in a single line like this:
// actions.doubleClick(dblclick).perform();
System.out.println("Double click action performed successfully on an element");
// Switching to Alert from the web page.
Alert alert = driver.switchTo().alert();
// Capture the text displayed on pop up and save it in a variable getText of type String.
String getText = alert.getText();
System.out.println("Alert text: " +getText);
// Click on OK button.
alert.accept();
}
}Output:
Double click action performed successfully on an element
Alert text: You double clicked me.. Thank You..Explanation:
1. In the preceding code, by creating an object of actions class, we have called doubleClick() method of actions class to perform a double-click on an element.
2. The doubleClick() method needs an element as an input parameter on which double-click will be fired. Therefore, we passed an element (dblclick) as an input parameter.
3. After executing the above code, you should see an alert message box saying that you double clicked me. Thank you.
4. We can also switch to an alert message box to capture message by using the Alert interface.
5. alert.getText() has been used to get the text from the alert message dialog box.
6. alert.accept() is used to press the OK button on the alert dialog box.
Recommended Tutorial:
In this tutorial, we have covered almost all important points related to how to handle or perform double click action on an element as well as the current location in Selenium WebDriver. Hope that you will have understood this easy topic and performed practically. In the next, we will understand how to perform right click action on a web element in Selenium.
Thanks for reading!!!








