Selenium Actions: Handle Double & Right Click In Selenium

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

In this tutorial, we will explore how to Handle Double and Right Mouse Clicks in Selenium Web Driver using Selenium Actions Class:

A computer mouse has 2 click buttons, one is the left-click, and the other is the right-click button.

Double click is the process of clicking the left mouse button twice. This click is usually performed with the left mouse button and it opens a new tab, a new folder that executes a file, a folder, or a program.

Right-click is the process of single-clicking the right mouse button. It gives options to open a file, folder, or program.

=> Check ALL Selenium Tutorials Here

Handling mouse clicks in Selenium – Double Click, Right Click

In this tutorial, we will explore the ways in which these clicks are handled in Selenium.

Double Click And Right-Click Buttons In HTML

We can see various web pages where examples of Double click and Right-click can be found. Given below is one such example of the HTML page that we have created, it shows a Double click and a Right-click button.

Double click

The above image shows a Double click button and a Right-click option. When the double click button is clicked twice, a message pops up for double click. Whereas, on clicking the Right-click option – a list of actions such as open link in a new tab, open link in a new window, etc are displayed.

Check the below HTML code and the images following it for more information.

<html>
<head>
            <title> Clicks Demo </title>
</head>

<div> 
<div>

<button id="doubleclick" ondblclick="callOnDoubleClick()">Double-click this box </button>

<script>
function callOnDoubleClick()
{
alert("Great ! \n\n Double-click is performed successfully..");
}
</script>

</div>
</div> <br>

<div>
<a href="file:///E:/Selenium%20class/Programs/Sonali/bin/project1/add.html" id="rightclick"
 oncontextmenu="callOnRightClick(event)">Right-click here</a>
</div>

</html>

HTML code for the tab opened after clicking the Right-click option.

<html>

<head> 
            <title> HTML Page </title>
</head>

<body>
<form>
<h2> Mouse click </h2>

Great!

Right Click is performed successfully. Also, desired tab/window is opened.

</form>
</body>

</html>

Pop up after Double click:

pop up after clicking double click.

Options displayed after Right-click:

click_html

html_right

The below tab opens after choosing “Open link in a new tab”:

Open link in new tab

Actions Class In Selenium

Code For Handling Double Click In Selenium

After understanding Double click and Right-click on an HTML page, let’s see how they are handled in Selenium.

Let’s consider an example of a link, as shown in the below screenshot, where we will be handling these clicks using Selenium.

Handling Clicks in Selenium

Initially, when logged into the website with the username and password, the above dashboard page opens up. Our motive is to perform Double click and Right-click operations on the Admin tab.

Let’s see the implementation of the code for handling Double click operation.

package SeleniumPrograms;

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 DoubleClick {

    public static void main(String[] args) throws InterruptedException {

        WebDriver dr = new FirefoxDriver();
        dr.manage().window().maximize();
        dr.get("https://opensource-demo.orangehrmlive.com/");	            //testing webpage
         
         WebElement uname = dr.findElement(By.id("txtUsername"));	 //username
         uname.sendKeys("Admin");
         
         WebElement pwd = dr.findElement(By.name("txtPassword"));       //password
         pwd.sendKeys("admin123");
         
         WebElement login_button = dr.findElement(By.xpath("//input[@id='btnLogin']"));
         login_button.click();							//loginbutton
		
         WebElement admin = dr.findElement(By.id("menu_admin_viewAdminModule"));
         Actions act = new Actions(dr);
         Thread.sleep(3000);
         act.doubleClick(admin).build().perform();		        //Double Click = Left Click)
         Thread.sleep(3000);
      }
}

The above program code for Double click shows the use of Actions class. In this code, Double click is handled in Selenium using the Actions class. This is done by creating an object of the Actions class through bypassing the driver.

Actions Class to handle Double click in Selenium

The web element is considered on which we want to perform Double click (here it is “Admin” tab) and then using the Actions class and in-built double-click method, the Double click operation is performed on the web element.

What happens after the Double Click Operation?

When the “Admin” tab is Double clicked, it opens another page and the opened page can be viewed i.e. the “System Users” page. Thus, on Double-clicking the Admin’s tab we are directed to the System Users page.

Check the below image for better understanding.

Double Click Admin Tab

Code For Handling Right-Click In Selenium

So far, we have got a clear idea of handling Double click in Selenium, now let’s see the handling of Right-click using Selenium. Again let’s consider the same example and perform Right-click on the “Admin” tab.

Let’s see the implementation of the code for handling the Right-click operation.

package SeleniumPrograms;

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 RightClick {

     public static void main(String[] args) throws InterruptedException {

        WebDriver dr = new FirefoxDriver();
        dr.manage().window().maximize();
        dr.get("https://opensource-demo.orangehrmlive.com/");	            //testing webpage
		
         WebElement uname = dr.findElement(By.id("txtUsername"));	   //username
         uname.sendKeys("Admin");
         
         WebElement pwd = dr.findElement(By.name("txtPassword"));	  //password
         pwd.sendKeys("admin123");
         
         WebElement login_button = dr.findElement(By.xpath("//input[@id='btnLogin']"));
          login_button.click();							//loginbutton
			
         WebElement admin = dr.findElement(By.id("menu_admin_viewAdminModule"));
         Actions act = new Actions(dr);
         Thread.sleep(3000);
         act.contextClick(admin).build().perform();		         //Right Click = Single Click
         Thread.sleep(3000);
       }
}

In Selenium, Right-click is also called a Context-click. As seen in the above code Right click is performed on the “Admin” tab using the Actions class.

Actions Class to handle Right-click in Selenium

The web element is considered on which we want to perform Right-click (here it is “Admin” tab) and then using the Actions class and in-built right-click method, the Right-click operation is performed on the web element.

What happens after the Right-click operation?

When the “Admin” tab is Right clicked, it shows a list of actions to choose from.

Thus, on Right-clicking the Admin’s tab we get the following options:

  1. Open link in new tab
  2. Open link in new window
  3. Open link in a new private window
  4. Bookmark the link
  5. Save the link
  6. Copy link location
  7. Search Google for “Admin” and
  8. Open element Inspector

Check out the below image for Reference:

Page after Right Click operation

Examples/Applications Where Double Click Is Commonly Used

#1) Opening A Folder

Folder to be opened: “SeleniumPrograms”.

Opening a Folder

On performing Double click on the “SeleniumPrograms” folder, the folder opens up and the files and the folders available inside the opened folder are visible as shown in the below image.

Folders inside the opened folder

#2) Open A File

As shown in the below image: demofile1 is to be opened.

Using Double-click operation, the excel file can be seen open. In the same way, using Double click operation, we can open any document i.e. Word document, Excel document, PowerPoint Presentation, etc.

Opening a File

#3) Uninstalling An Application

Open the Control Panel on your computer and then go to Programs and Features.

Now you will see a list of programs or applications in your computer system. The programs we wish to uninstall can be uninstalled by simply Double-clicking on them.

For example:

uninstalling an application

On Double-clicking any program to be uninstalled, it will ask for confirmation and we will be able to uninstall it.

Here, we have tried to uninstall the “Cisco PEAP Module” and we get a pop-up message for confirmation of uninstalling the selected program. If you select NO – you will again be on the same window and if you select YES – you will be able to uninstall the selected program.

Thus, the Double click operation performed on an element helps the user to directly open or act on it.

Examples/Applications Where Right Click Is Commonly Used

Given below are a few examples on which the Right-click is performed.

#1) Folder

When the mouse cursor is moved to a folder and Right click is performed on it, the options as shown in the below image are displayed.

Right-Click to open a folder

In this way, when Right-click is performed on any folder/zip folder, various options would be shown and the user would perform any one as his next action.

#2) File Editing Actions

Open the file. Right-click operation on any file allows the users to perform various operations on it.

Check the below example for a clear understanding:

File Edit - Right Click

We can notice that the Right-click operation performed on cell D4 of the excel file provides a list of actions to perform on it.

#3) Uninstalling An Application

In the “Programs and Features” section of the “Control Panel”, all the applications installed in our system are shown. Here, the user has options to uninstall, change, or repair an application, and these operations are received by Right-clicking on that particular application.

uninstall_right click

Thus, Right-click operation provides a list of options using which the user can choose one option to perform further required actions.

Difference Between Double-Click And Right-Click

Sr. No.Double clickRight Click
1Double click is nothing but performing Left mouse click twice.Right click is performing a single Right mouse click.
2This directly interacts with an object.This indirectly interacts with an object.
3 Directly opens the files, links or any of the clicked elements. It has hidden options, these are opened using something else
4It is also known as normal click or regular click.It is also known as Context Click.
5Example: Double clicking of a link opens it immediately after the click.Example: Right clicking of a link shows options to act on the link like Open link in new tab, opening in new window, copy link, save link, etc.

Conclusion

We explored several examples of Double click and Right-click, where the clicking of the left mouse button twice opens the element that we have performed double click on. Whereas, performing Right mouse click on any element – lists out different actions to carry out further.

In this tutorial, we understood Double and Right mouse clicks in detail- we saw how they are used in HTML Pages, their handling in Selenium using Actions class along with the differentiation between Double click & Right-click and examples found in our daily work.

=> Read Through The Complete Selenium Guide

Was this helpful?

Thanks for your feedback!

Leave a Comment