Double Click and Right-Click in Selenium Webdriver

Double Click and Right-Click in Selenium Webdriver

Table of Contents

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

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

You can see various web pages where you can find examples of Double click and Right-click can be found. One example of the HTML page that we have created is given below that shows a Double click and a Right-click button.

Double click

The image above shows a Double click button and a Right-click option. While the double click button is clicked twice, a message pops up for double click. After clicking the Right-click option, a list of actions like open link in a new tab, open link in a new window, etc., is displayed.

Check the below code and the images.

<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 performed successfully..");
}
</script>
</div>
</div> <br>
<div>
<a href="file:///E:/Selenium class/Programs/Sonali/bin/project1/add.html" id="rightclick"
 oncontextmenu="callOnRightClick(event)">Right-click here</a>
</div>
</html>
HTML code is given below 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 that opens after Double click:

pop up after clicking double click.

Options that are displayed after Right-click:

click_html
html_right

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

Open link in new tab

Actions Class In Selenium:

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

Consider a scenario of a link, as shown in the below screenshot, where we will handle these clicks using Selenium.

Handling Clicks in Selenium

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

Let us see the implementation of the code to handle Double click operation.

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 arg[]) 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(); 
         Thread.sleep(3000);
      }
}

The above code for Double click shows the use of the Actions class. In the above code, Double click is handled in Selenium using the Actions class and is done by creation of an object of Actions class by bypassing the driver.

Actions Class for handling Double click in Selenium:

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

What happens after a Double Click Operation?

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

Check the image for a better understanding.

Double Click Admin Tab

Code For Handling the Right-Click In Selenium:

Now let us see the handling of Right-click using Selenium Webdriver. Again let us consider the same example and perform Right-click on the “Admin” tab.

Let us see the implementation of the code to handle the Right-click operation.

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 arg[]) 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();   
         Thread.sleep(3000);
       }
}

In Selenium Webdriver, Right-click is also called a Context-click. The above Right click is performed on the “Admin” tab using the Actions class.

Actions Class for handling Right-click in Selenium:

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

What happens after a Right-click operation?

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

Thus, after 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

Take a look at the image below:

Page after Right Click operation

Difference Between Double-Click And Right-Click:

Double clickRight Click
Double click is nothing but performing a Left mouse click twice.Right-click is performing a single Right mouse click.
This directly interacts with an object.This indirectly interacts with an object.
It is also known as a normal click or regular click.It is also known as Context Click.
Example: Double-clicking of a link opens it immediately after the click.Example: Right-clicking a link shows options to act on the link like Open link in a new tab, opening in a new window, copy link, save the link, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class