Double Click On Button Using Actions Class Of Selenium WebDriver

As you know, We can use WebDriver Actions class where we need to perform series of actions to complete operation. We have learnt many tutorials on usage of Actions class In selenium webdriver to perform such tricky advanced user Interactions like drag and drop element, selecting JQuery selectable Items, Moving slider, selecting date from JQuery date picker etc.. You can view all these advanced user Interactions tutorials links on THIS PAGE with practical examples on each one.

Same way, Double clicking on button Is series of actions as you needs to click two time on button. Simple single button click Is possible by click() method In webdriver but to perform double click action, we need to use Actions class of selenium webdriver. You can learn how to right click on select option from context menu In THIS POST.

I have created practical example to show you how to use Actions class with doubleClick() and perform() methods to double click on element.

package Testing_Pack;

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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DoubleClick {
 WebDriver driver;
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 public void doubleClick_Button() throws IOException, InterruptedException {
  WebElement ele = driver.findElement(By.xpath("//button[contains(.,'Double-Click Me To See Alert')]"));
  //To generate double click action on "Double-Click Me To See Alert" button.
  Actions action = new Actions(driver);
  String alert_message = driver.switchTo().alert().getText();

When you run example, It will double click on "Double-Click Me To See Alert". Rest of code Is to handle alert generated by button double click. So this Is the way to double click on any element In selenium webdriver test.