How to Perform Right-Click using Java in Selenium?
Last Updated :
28 Apr, 2025
While automating a website for testing there is always required to perform some right-click or other user actions on the page. These user actions are one of the most commonly used actions during automation, so selenium provides a way to perform these user actions by the Actions class.
How to Perform Right Click using Actions Class
When a user performs a right click using the mouse on a particular element to perform some actions is called a right click. We are daily using this user action mostly on File Explorer, For example, to rename a file or delete a file we perform right-click and select an option.
Right Click in Selenium
Let's see how to perform the right click using Selenium. Selenium Webdriver API does not support the user's actions like Mouse hover, right-click, context-click, and double-click. That is where the Actions class came to use, The actions provided by this class are performed by an API called Advanced user interaction in selenium webdriver.
Action class present in the package,
"org.openqa.selenium.interactions package"
Let's see how to use the Actions class to Right Click an element:
Instantiate an object for the Actions class
Actions action = new Actions(driver);
After creating the object we have to locate the web element
WebElement element=driver.findElement(locator);
Using the "ContextClick() method" from the Actions class to perform the Right click. Context Click methods navigate the mouse pointer to the middle of the web Element and then perform the right-click action in that web element.
action.contextClick(webElement).perform();
Example
In this example, we are navigating to the URL "https://demoqa.com/buttons" and performing the Right click on the "Right click" button.
Java
public class Geeks {
public void geekforgeeks()
{
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/buttons");
WebElement element
= driver.findElement(By.id("rightClickBtn"));
Actions action = new Actions(driver);
action.contextClick(element).perform();
Thread.sleep(5000);
driver.close();
}
Code Explanation
Initially, we opened the browser and navigated to the URL
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/buttons");
After that, we locate the web element where we have to perform the "Right Click". Then, We initialize the Action class and performed "Right click" on the web element.
Actions action=new Actions(driver);
action.contextClick(element).perform();
Output
Right-click is performed and the result will be displayed.
Similar Reads
How to click on an image using Selenium in Java? Selenium, a popular tool for automating web application testing across browsers, often requires image interaction. In this article we will discuss how to clicking on image using Selenium in Java.PrerequisitesJava Development Kit (JDK) installed.Selenium WebDriver library added to your project.A supp
3 min read
How to Run Gecko Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. It consists of three parts: Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium WebDriver is the most important. Using WebDriver, online website testing can be done. There are three main WebDriver implementations:ChromeD
5 min read
How to Handle Alert in Selenium using Java? Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
5 min read
How to Take a Screenshot in Selenium WebDriver Using Java? Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
How to Select Multiple Elements using Actions Class in Selenium using Java? Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a web page that has file upload or other functionality which requires selecting multiple elements, to perform the multiple select actions the selenium provides a class called Acti
2 min read
How to Submit a Form using Selenium? Selenium is a great tool when it comes to testing the User interface of websites. Because it has so many features like web driver it allows us to write the scripts in different programming languages like Java, Python, C#, and Ruby. We can write scripts concerning different browsers including the maj
7 min read