
πRight Click and Double Click in Selenium WebDriver
Right Click and Double Click in Selenium: In Selenium automation, performing mouse interactions like Right Click (Context Click) and Double Click is essential for handling advanced user actions. Selenium provides these capabilities using the Actions class.
In this tutorial, you’ll learn:
- How to perform Right Click using Selenium
- How to perform Double Click
- Code examples using Java
- Explanation of Actions class methods
π Table of Contents
- What is Actions Class in Selenium?
- How to Perform Right Click in Selenium
- Real-Time Test Scenario for Right Click
- How to Perform Double Click in Selenium
- Example for Double Click
- Summary
πWhat is Actions Class in Selenium?
Right Click and Double Click in Selenium: The Actions class in Selenium WebDriver provides advanced user interactions like:
- Mouse hover
- Drag and drop
- Right click (Context Click)
- Double click
- Click and hold
To use these actions, you need to import:
java
import org.openqa.selenium.interactions.Actions;
You must create an Actions object and call the required method to perform the action.
π How to Perform Right Click in Selenium?
β Method Used: contextClick()
Right Click and Double Click in Selenium: Right Click is also known as Context Click in Selenium. The contextClick() method is used to right-click on a specific element.
Test Scenario:
- Launch the URL
- Perform right-click on the button: βright click meβ
- Click on the Edit option from the context menu
- Handle the Alert by clicking OK
- Close the browser
πCode Example β Right Click:
java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickExample {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(“https://swisnl.github.io/jQuery-contextMenu/demo.html”);
driver.manage().window().maximize();
// Locate the element to right-click
WebElement button = driver.findElement(By.cssSelector(“.context-menu-one”));
// Create Actions class object
Actions action = new Actions(driver);
// Perform right click
action.contextClick(button).perform();
// Click on the ‘Edit’ option
WebElement editOption = driver.findElement(By.cssSelector(“.context-menu-item.icon-edit”));
editOption.click();
// Handle the alert
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
// Close browser
driver.quit();
}
}
πHow to Perform Double Click in Selenium?
β Method Used: doubleClick()
Right Click and Double Click in Selenium: To perform a double-click operation on a web element, Selenium provides the doubleClick() method of the Actions class.
π Code Example β Double Click:
java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DoubleClickExample {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(“https://demo.guru99.com/test/simple_context_menu.html”);
driver.manage().window().maximize();
// Locate the element to double click
WebElement button = driver.findElement(By.xpath(“//button[text()=’Double-Click Me To See Alert’]”));
// Create Actions class object
Actions action = new Actions(driver);
// Perform double click
action.doubleClick(button).perform();
// Handle alert
Alert alert = driver.switchTo().alert();
System.out.println(“Alert message: ” + alert.getText());
alert.accept();
// Close browser
driver.quit();
}
}
π Summary
Action | Method Used | Use Case |
Right Click | contextClick() | Open context menu, select options |
Double Click | doubleClick() | Trigger events that require double-click |
Use Actions class for advanced mouse interactions- Always use .perform() at the end to execute the action
- Combine with Alert, By, and WebElement for full test coverage