0
0
Selenium Javatesting~5 mins

Double click in Selenium Java

Choose your learning style9 modes available
Introduction

Double click simulates a user clicking twice quickly on an element. It helps test features triggered by double clicks.

When a button or link reacts differently on double click than single click.
To test text selection or editing triggered by double clicking.
When a menu or popup opens only after double clicking an item.
To verify drag and drop or special actions that start on double click.
Syntax
Selenium Java
Actions actions = new Actions(driver);
actions.doubleClick(WebElement element).perform();

Use the Actions class to perform complex mouse actions like double click.

Always call perform() to execute the action.

Examples
Double clicks on a button found by its ID.
Selenium Java
WebElement button = driver.findElement(By.id("myButton"));
Actions actions = new Actions(driver);
actions.doubleClick(button).perform();
Double clicks on a text input field using a CSS selector.
Selenium Java
WebElement textField = driver.findElement(By.cssSelector("input[type='text']"));
new Actions(driver).doubleClick(textField).perform();
Sample Program

This test opens a page, double clicks a box, and checks if the success message appears.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DoubleClickTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/doubleclickpage");
            WebElement box = driver.findElement(By.id("doubleClickBox"));
            Actions actions = new Actions(driver);
            actions.doubleClick(box).perform();
            String message = driver.findElement(By.id("message")).getText();
            if ("Double click successful".equals(message)) {
                System.out.println("Test Passed");
            } else {
                System.out.println("Test Failed");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Make sure the element is visible and enabled before double clicking.

Double click speed depends on the system; Selenium handles it internally.

Use explicit waits if the element loads dynamically.

Summary

Double click simulates two quick clicks on an element.

Use Actions class and call perform() to execute.

Useful to test UI features triggered by double clicks.