How to handle Mouse Actions In Selenium?

Reading Time: 4 minutes

Hello People, Nowadays Selenium has become the industry standard for implementing custom automated tests because of which it is considered the first go-to solution for every web application. Action Class in Selenium is a built-in feature provided by the selenium for handling keyboard and mouse events. This is something very interesting part of Selenium which is performed using the advanced user interaction API in the Selenium Web driver. On that note, I’m writing this article on how to handle the Actions class in Selenium to help you understand how actions work.

What is the actions class in Selenium Webdriver?

Performing Page Interactions with Selenium Actions - TestProject
  • Actions class is a built-in ability to handle various types of keyboard and mouse events. It is specifically designed to handle mouseover and keyboard interactions with the user. Apart from that, we use this class to simulate the user’s gestures on the application. However, this class provides specific methods to handle actions in an application which you generally do by mouse or keyboard.
  • We know Selenium methods to click but we haven’t seen moving our mouse towards a specific element. These kinds of mouse interactions, for example, Context-click which means right-click, double click, drag, and drop, etc are some of the methods of action class.
  • The Actions class is the user-facing API for emulating complex action events. You can directly use this class rather than using the input devices, i.e. keyboard and mouse.

Mouse Interface

You'd Have to Click a Mouse 10 Million Times to Burn One Calorie | WIRED
  • click(): Clicks on the element.
  • double Click (): Double clicks on the element.
  • context Click() : Performs a context-click (right-click) on the element.
  • click and hold(): Clicks at the present mouse location without releasing.
  • moveToElement(toElement): It shifts the mouse to the center of the element.

Procedure to Handle Action Class

To handle actions, create an object of class Action. Then Perform actions on the element and use build().perform() to perform compilation and execution. Also, use different methods to perform different actions and operations.

Methods of Action Class

let’s see how to define an action class and how to move these methods to perform our actions.

Some Code Snippets of Action Class in Selenium

Click Action In Mouse Action Class

Code Snippet:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
    public class actionsDemo {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver");
            WebDriver driver = new ChromeDriver();
            // Resizing the window
            driver.manage().window().maximize();

            // Navigate to Url
            driver.get("https://www.knoldus.com/home");

            ((JavaScript) driver).executeScript("scroll(0,300)");
            //Performing Click mouse action
            Actions ac = new Actions(driver);   
            WebElement automate= driver.findElement(By.linkText("Services"));
            ac.moveToElement(automate).click();
            driver.close();
        }
    }

Mouse Hover Action In Mouse Action Class

Code Snippet:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
    public class hoverAction {
        public static void main(String[] args){
            System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver");
            WebDriver driver = new ChromeDriver();

           // Resizing the window
            driver.manage().window().maximize();

            // Navigate to Url
            driver.get("https://www.knoldus.com/home");
            ((JavascriptExecutor) driver).executeScript("scroll(0,300)");

            //Performing MouseHover Action
            Actions ac = new Actions(driver);
            WebElement automate= driver.findElement(By.linkText("Services"));
            ac.moveToElement(automate).click();
            driver.close();
        }
    }

Double Click Action In Mouse Action Class

Code Snippet:

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 ActionsDoubleClickDemo {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver");
            WebDriver driver = new ChromeDriver();
            // Resizing the window
            driver.manage().window().maximize();

            //Navigate to URL
            driver.get("https://www.google.com/"); 

            //Double click on element
            Actions action = new Actions(driver);
            WebElement webElement=driver.findElement(By.linkText("Sign in"));
            action.doubleClick(webElement).perform();
            driver.close();
        }
    }
}

Context Click Action In Mouse Action Class

Code Snippet:

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) {
            System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver");
            WebDriver driver = new ChromeDriver();

                // Navigate to Url
                driver.get("https://google.com");

                // Store 'google search' button web element
                WebElement search =driver.findElement(By.linkText("Signin"));
                Actions context = new Actions(driver);

                // Perform context-click action on the element
                context.contextClick(search).build().perform();
            driver.quit();
            }
    }
}

Click And Hold Action In Mouse Action Class

Code Snippet:

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 clickAndHold {
  public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
     driver.get("https://google.com");
      WebElement searchBtn = driver.findElement(By.linkText("Sign in"));

// Perform click-and-hold action on the element
      Actions actionProvider = new Actions(driver);
      actionProvider.clickAndHold(searchBtn).build().perform();
      driver.quit();
    }
  }
}
  

Using Action class in Selenium is of utmost importance. This article simplifies the process so that people know how to simulate common user actions on websites and applications. This lets them monitor software behavior in the real world so that they can verify and optimize user experience for its best possible state.

 

To sum it up, we have seen what is Actions class in Selenium is and the different methods that are used to interact with a browser. We have seen examples for performing different actions.

For more references visit:

https://www.selenium.dev/documentation/webdriver/actions_api/mouse/


Knoldus-blog-footer-image