Performing Mouse Double Click using Selenium Web Driver

Some web applications may need to perform double click to fire the action. Following code will help how to implement Mouse Double Click using Selenium.

In the following example, page contans a div and if we perform single click noting is going to happen but when we perform double click Map will be zoomed.


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.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DoubleClick {
 public String baseURL = "http://openlayers.org/dev/examples/click.html";
 public WebDriver driver = new FirefoxDriver();
 public WebElement element;
 public String title;
 public Actions action = new Actions(driver);
 @BeforeTest
 public void launchBrowser()
 {
  driver.get(baseURL);
 }

 @AfterTest
 public void closeBrowser()
 {
  driver.quit();
 }

 @Test
 public void doubleClick() throws InterruptedException
 {
  element = driver.findElement(By.xpath(".//*[@id='OpenLayers_Map_2_OpenLayers_ViewPort']"));
  element.click();// For single click Map will not load
  System.out.println("Single Click performed");
  Thread.sleep(3000);
  Action doubleClick = action.doubleClick(element).build();
  doubleClick.perform();// After performing double click Map will load
  System.out.println("Double Click performed");
  Thread.sleep(10000);
  doubleClick.perform();// After performing double click Map will load
  System.out.println("Double Click performed");
  Thread.sleep(10000);
 }
}