---Advertisement---

Right Click and Double Click in Selenium WebDriver: Actions Class Examples Best 2025

By Manisha

Updated On:

---Advertisement---
Right Click and Double Click in Selenium

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

  1. What is Actions Class in Selenium?
  2. How to Perform Right Click in Selenium
  3. Real-Time Test Scenario for Right Click
  4. How to Perform Double Click in Selenium
  5. Example for Double Click
  6. Summary

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.


βœ… 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:

  1. Launch the URL
  2. Perform right-click on the button: β€œright click me”
  3. Click on the Edit option from the context menu
  4. Handle the Alert by clicking OK
  5. Close the browser

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

    }

}


βœ… 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.


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

    }

}


ActionMethod UsedUse Case
Right ClickcontextClick()Open context menu, select options
Double ClickdoubleClick()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

Testing Selenium Download

Master XPath in Selenium

Leave a Comment

Index