Skip to content
CodeKruPro

Double Click Action

52/70

Double Click Action

actions.doubleClick(element) simulates a double click.

On the Buttons page, the double-click button (id="doubleClick") changes text to "I was double-clicked!" when double-clicked.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the doubleClick button and print its initial text
  3. Double-click it using the Actions class
  4. Print its text after double-clicking

Expected output:

Before: Double click on me
After: I was double-clicked!

About this lesson

Double click an element in Selenium Java with Actions doubleClick()

Learn Selenium JavaAdvancedActions Class

How it works

A double click is its own browser event, dblclick, fired when two clicks land close together in time. actions.doubleClick(element).perform() produces it. Calling element.click() twice does not: on the Buttons page two separate click() calls leave the double-click button's label unchanged, because each call is a separate WebDriver command and they are not treated as one gesture.

Inside a single Actions chain timing is tight enough, so actions.click(element).click(element).perform() does register as a double click. doubleClick() says what you mean, though, and is the call to use.

doubleClick() with no argument double-clicks wherever the pointer currently is. Passing the element moves the pointer there first, which is almost always what you want.

About the assignment

The starter gives you an Actions object and the double-click button. You read its label, double-click it, and read the label again to see the page react to the dblclick event.

What you will do

  1. Print the button's text after the label Before:.
  2. Double-click it with doubleClick() and perform().
  3. Print its text again after the label After:.

Common mistakes

  • Calling click() twice

    The label does not change, because two separate commands are not a double click.

  • Leaving out perform()

    Nothing is sent to the browser and the after line repeats the before line.

  • Double-clicking without the element argument

    doubleClick() alone acts at the current pointer position, which in a fresh session is not over the button.

In a real test suite

Double clicks open files in web file managers, enter edit mode in spreadsheet-style grids, and select words in editors. They are less common than single clicks, so tests that need them usually say so in a helper name such as editCell().

Lesson 51 hovered. Lesson 53 right-clicks the button next to this one.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        Actions actions = new Actions(driver);
        WebElement button = driver.findElement(By.id("doubleClick"));
        
        // TODO: Print initial text
        // TODO: Double-click the button
        // TODO: Print text after double-click
        
        driver.quit();
    }
}