How to Right Click on WebElement in Selenium

Right-click action in Selenium | In the previous tutorial, we have successfully performed double-click action on a WebElement and current location on the web page.

Now, moving ahead to the next action called right-click or context-click. Basically, a context menu is a menu displayed on a web page that appears when a user performs right-click of the mouse on a WebElement.

This menu is also called pop-up menu that appears quite common on a web page these days.For example, a context menu is shown in the below screenshot that appears on performing a right-click operation on a WebElement.
Right click in Selenium
An actions class of Selenium WebDriver provides a method called contextClick() which is used to perform right-click operation. This method comes into two variants. First is, for right-clicking on current location and second is, for right-clicking on a WebElement.

The syntax of contextClick() method to click on WebElement is as follows:

public Actions contextClick( WebElement onElement)

This method takes an input parameter as WebElement that has to be right-clicked. Its return type is actions instance.


Let’s automate a test scenario where we will open a context menu by clicking on WebElement and select one of the menu options using the actions class.

Scenario to Automate:

1. Launch the Firefox browser by creating a WebDriver object.
2. Open a URL “https://selenium08.blogspot.com/2019/12/right-click.html”.
3. Locate WebElement by By.xpath.
4. Click on the context menu.
5. Click on the element “Python” of the context menu.
6. Switch to simple alert pop-up.
7. Get the text displayed on the pop-up.
8. Click on the OK button to accept pop-up.

Let’s understand the program code related to the above scenario.

Program code 1:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 

public class RCActionWebElement { 
public static void main(String[] args) 
{ 
// Create a driver object to launch Firefox browser. 
   WebDriver driver = new FirefoxDriver(); 

// Maximize the browser. 
   driver.manage().window().maximize(); 

// Create a variable URL of string type to store the URL. 
   String url = "https://selenium08.blogspot.com/2019/12/right-click.html"; 

// Call get() method of WebDriver and pass URL as a parameter. 
   driver.get(url); 

// Wait for some time to load web page. 
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

// Locate the WebElement by By.xpath. 
   WebElement contextMenu = driver.findElement(By.xpath("//div[@id = 'div-context']")); 

// Create an object of Actions class and pass reference variable driver as a parameter to its constructor. 
   Actions actions = new Actions(driver); 
   actions.contextClick(contextMenu); 

// Locate an element "Python" by By.xpath in the context menu. 
   WebElement python = driver.findElement(By.xpath("//a[text() = 'Python']")); 

// Call click(WebElement) method and pass element as an input parameter. 
   actions.click( python ); 
   actions.perform(); 
   System.out.println("Right-clicked Successfully on Context menu"); 

// Switch to alert pop-up from web page by using below syntax. 
   Alert alert = driver.switchTo().alert(); 

// Get text displayed on pop-up. 
   String getText = alert.getText(); 
   System.out.println("Displayed Text on pop-up: " +getText); 

// Call accept() method of alert interface to click OK button to accept pop-up. 
   alert.accept(); 
   driver.close(); 
   } 
 }
Output: 
       Right-clicked Successfully on Context menu 
       Displayed Text on pop-up: Python Clicked

In the above code, first, we have right-clicked action using contextClick() method on a WebElement “contextMenu” and then left-click action on “Python” from the contextMenu. After left-clicking, a simple alert pop-up message will be displayed on the screen saying Python Clicked.

How to Context Click at Current Location in Selenium?


In the previous section, we have seen context-click at WebElement. Now, we will move to context-click at current mouse location. To context click at current location, we will use contextClick() method. The syntax for contextClick() method is given below:

public Actions contextClick()

The contextClick() method does not accept any input parameter and returns actions instance.

Let’s understand the necessary modifications needed in the previous example scenario to use this method. The following program code is given below. Follow all the above steps as shown in the previous program code.

Program code 2:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 

public class RCActionCurrentLocation { 
public static void main(String[] args) 
{ 
   WebDriver driver = new FirefoxDriver(); 
   driver.manage().window().maximize();
 
// Create a variable URL of string type to store the URL. 
   String url = "https://selenium08.blogspot.com/2019/12/right-click.html"; 

// Call get() method of WebDriver and pass URL as a parameter. 
   driver.get(url); 
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

// Locate the WebElement by By.xpath. 
   WebElement contextMenu = driver.findElement(By.xpath("//div[@id = 'div-context']")); 

// Create an object of Actions class and pass reference variable driver as a parameter to its constructor. 
   Actions actions = new Actions(driver); 

// Call moveToElement() method to move the mouse cursor from initial position (0,0) to element contextMenu. 
   actions.moveToElement(contextMenu); // Passing contextMenu as an parameter. 
   actions.contextClick(); // Calling contextClick() method to right click on context menu. 

// Locate an element "Java" by By.xpath in the context menu. 
   WebElement java = driver.findElement(By.xpath("//a[text() = 'Java']")); 

// Call click(WebElement) method and pass element as an input parameter. 
   actions.click( java ); 
   actions.perform(); 
   System.out.println("Right-clicked Successfully on Context menu"); 

// Switch to alert pop-up from web page by using below syntax. 
   Alert alert = driver.switchTo().alert(); 

// Get text displayed on pop-up. 
   String getText = alert.getText(); 
   System.out.println("Displayed Text on pop-up: " +getText); 

// Call accept() method of alert interface to click OK button to accept pop-up. 
   alert.accept(); 
   driver.close(); 
  } 
 }
Output: 
       Right-clicked Successfully on Context menu 
       Displayed Text on pop-up: Java Clicked

In the preceding code, first, moved the mouse cursor to the WebElement and then have been performed context-click on it.


In this tutorial, we have covered all important points related to how to perform or handle right click action on WebElement & current location in Selenium WebDriver. Hope that you will have understood this topic and performed practically. In the next, we will learn how to perform click and hold in Selenium WebDriver.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love