Right Click and Double Click in Selenium

โšก Smart Summary

Right Click and Double Click in Selenium are mouse actions automated through the Actions class. This tutorial demonstrates both operations with working Java code, real test scenarios, and the methods that drive them inside Selenium WebDriver.

  • ๐Ÿ–ฑ๏ธ Core Method: Actions class exposes contextClick() for right click and doubleClick() for double click on any WebElement.
  • ๐Ÿงญ Workflow Pattern: Locate the element, instantiate Actions with the driver, call the action, and chain perform() to execute.
  • ๐Ÿงช Tested Scenario: Both examples target demo.guru99.com/test/simple_context_menu.html for verified, repeatable results.
  • ๐Ÿ†š Action Distinction: Right click opens contextual menus; double click triggers state changes such as alerts or edits.
  • ๐Ÿค– AI Integration: Self-healing locators and AI-assisted Selenium frameworks reduce flakiness during click actions.

Right Click and Double Click in Selenium

Right Click in Selenium

Right click action in Selenium WebDriver is done using the Actions class. The operation is also called Context Click. The predefined contextClick() method of the Actions class performs the right click. Below is the basic syntax.

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

How to Right Click in Selenium

The scenario below launches a Guru99 demo page, performs a right click, and selects an option from the context menu that appears.

Test Scenario:

  1. Launch: https://demo.guru99.com/test/simple_context_menu.html
  2. Right Click on the button “right click me”
  3. Click the Edit link on the displayed menu
  4. Click OK on the alert
  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();

        driver.get("https://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();

        Actions action = new Actions(driver);
        WebElement link = driver.findElement(By.cssSelector(".context-menu-one"));
        action.contextClick(link).perform();

        WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy"));
        element.click();
    }
}

Result: The context menu appears and the Edit option is selected.

Right Click in Selenium

Double Click in Selenium

Following the same Actions-based pattern, Double Click in Selenium WebDriver uses the predefined doubleClick() method. The Actions class is the standard helper for compound mouse and keyboard operations such as Right Click, Drag and Drop, and Hover.

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

The execution flow is:

  • Instantiate an Actions object using the driver instance.
  • Locate the target element with findElement.
  • Call doubleClick() and chain perform() to execute.

How to Double Click in Selenium

The next scenario demonstrates a full double click that triggers a JavaScript alert and confirms it programmatically.

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 DoubleClickDemo {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
        driver = new ChromeDriver();

        driver.get("https://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();

        Actions action = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
        action.doubleClick(link).perform();

        Alert alert = driver.switchTo().alert();
        System.out.println("Alert Text\n" + alert.getText());
        alert.accept();
    }
}

Result: The alert pops up and the alert text is printed to the Eclipse console.

Double Click in Selenium alert

Double Click console output

FAQs

WebElement.click() only fires a left-click. Right click and double click are compound mouse events that require the Actions class to chain and dispatch via perform().

Yes. contextClick() simulates a real right click and opens the application’s context menu. The two terms are used interchangeably in Selenium docs.

The Actions class queues events. Without perform(), the chained steps are never dispatched. perform() flushes the queue and executes each queued action in order.

Yes, JavaScript Executor can fire a double click, but it bypasses real browser events. The Actions approach is the recommended Selenium standard.

Yes. Right click and double click via Actions work in headless Chrome and Firefox provided the W3C Actions API is supported. Selenium 4 drivers fully support this.

The Actions class arrived in Selenium WebDriver 2.0 inside org.openqa.selenium.interactions and is retained in Selenium 3 and 4 with W3C Actions support.

AI tools and Selenium alternatives apply self-healing locators that adapt to element changes during clicks, reducing flaky failures from dynamic IDs or shifting layouts.

Yes. AI code assistants can convert a plain-English scenario into Selenium Java or Python code using contextClick() and doubleClick(), plus suggest robust locators and waits.

Summarize this post with: