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
- Print the button's text after the label
Before:. - Double-click it with
doubleClick()andperform(). - 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.
Related lessons
- Click a ButtonSingle clicks with
click(), which cannot double-click. - Right Click (Context Click) ActionRight-click with Actions on the same page.
- Mouse Hover ActionHow Actions chains and
perform()work.
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.
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();
}
}