Double click and Right Click in Selenium with Examples

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();

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.gtupapers.com/test/simple_context_menu.html");
driver.manage().window().maximize();

driver.get("http://demo.gtupapers.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.gtupapers.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.gtupapers.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:

 

YOU MIGHT LIKE: