Double click in Selenium

Double click action in Selenium web driver can be done using Actions class. Actions class is a predefined class in Selenium web driver used to perform multiple keyboard and mouse operations such as Right Click, Drag and Drop, etc.

Double click in Selenium using Actions class

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.doubleClick(elementLocator).perform();
  • Initially, we need to instantiate an object of Actions class by passing the driver instance as a parameter
  • Using find element command, we need to find the locator of an element that we want to double click
  • Using the pre-defined double click method of Actions class, we need to perform double click operation on the web element

Right-click in Selenium

Right click action in Selenium web driver can be done using Actions class. Right Click operation is also called Context Click in Selenium. Pre-defined method context click provided by Actions class is used to perform right click operation. Below is the code to demonstrate right click operation using Actions class.

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();

Example of Double Click

Test Scenario

Code:

package test;
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;
import org.openqa.selenium.Alert;

public class DobuleClickDemo {
public static void main(String[] args) throws InterruptedException {

	WebDriver driver;
	System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");
	 driver= new ChromeDriver();

//Launch the Application Under Test (AUT)
driver.get("http://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();

driver.get("http://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
//Double click the button to launch an alertbox
Actions action = new Actions(driver);
WebElement link =driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
action.doubleClick(link).perform();
//Switch to the alert box and click on OK button
Alert alert = driver.switchTo().alert();
System.out.println("Alert Text\n" +alert.getText());
alert.accept();
//Closing the driver instance
//driver.quit();

}
}

Result:

The button labeled "Double-Click Me to See Alert" is clicked and pop-up is shown

In Eclipse, you see the output in the console

Right Click Example

Test Scenario:

  1. Launch the URL: http://demo.guru99.com/test/simple_context_menu.html
  2. Perform Right Click operation on the button : right click me
  3. Click on Edit link on the displayed list of right click options
  4. Click on OK button on the alert displayed
  5. Close the browser

Code:

package test;
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 ContextClick {
public static void main(String[] args) throws InterruptedException {

	WebDriver driver;
	System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");
	 driver= new ChromeDriver();

//Launch the Application Under Test (AUT)
driver.get("http://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();

// Right click the button to launch right click menu options
Actions action = new Actions(driver);

WebElement link = driver.findElement(By.cssSelector(".context-menu-one"));
action.contextClick(link).perform();
// Click on Edit link on the displayed menu options
WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy"));
element.click();
// Accept the alert displayed
//driver.switchTo().alert().accept();
// Closing the driver instance
//driver.quit();

}
}

Result:

Summary:

  • Actions class in Selenium is mostly used to perform complex keyboard and mouse operations. Hence, Actions class is preferred compared to Javascript for performing operations such as Right Click and Double Click in Selenium.
  • Right click operation is mostly used when performing right click on an element opens a new menu. Right click operation in Selenium web driver can be done using the pre defined command Context Click as mentioned below
    Actions action = new Actions(driver);
    WebElement link = driver.findElement(By.ID ("Element ID"));
    action.contextClick(link).perform();
    
  • Double click operation is used when the state of web element changes after double click operation. Double Click operation in Selenium web driver can be done using the pre defined command Double Click as mentioned below
    Actions action = new Actions(driver);
    WebElement link = driver.findElement(By.ID ("Element ID"));
    action. doubleClick (link).perform();
    

 

YOU MIGHT LIKE: