Selenium WebDriver - Actions Class Mouse Events

In this tutorial we will learn Mouse event commands of Actions class of Selenium Webdriver.

 Mouse Events - Some of the mouse events that can be performed using Actions class are:
  • click()
  • doubleClick()
  • contextClick()
  • clickAndHold()
  • dragAndDrop()
  • moveToElement()
  • moveByOffset(x, y)
  • release()

1. Mouse Hover & Click
We usually come across some scenarios in automation of mouse hover like clicking on a sub-menu link which appears only after moving your cursor to menu link. It's quite a tricky thing to perform in automation. Thanks to the Actions Class in Selenium Webdriver.  We already have a Selenium command for it.
Code for Mouse Hover:
/* Mouse Hover

// Xpath for Menu
WebElement Menu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Xpath for Sub Menu
WebElement SubMenu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of Actions class
Actions actions = new Actions(driver);

// Move cursor to Menu link (Mouse hover on menu link so that sub menu is displayed)

// Click on Submenu link (whcih is displayed after mouse hovering curson on menu link);

Explanation of above code:
  1. Creating object of Actions class as actions. And passing driver object to constructor of Actions class.
  2. In second line, creating an object of WebElement and passing Xpath locator to it.
  3. moveToElement() is the command to perform mouse hover action.
  4. - This code is mandatory to finish any actions class command. build() method combines multiple actions into one single action and perform() executes those  actions.

2. Double Click
This command is used to double click on an element.

Code for Double Click:
/* Double Click
Actions actions = new Actions(driver);
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));

3. Context Click (Right Click)
This command is used to right click on an element.

Code for Context click (right click):
/* Right Click

// Locate web element
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of actions class
Actions actions = new Actions(driver);

// Right click on PRODUCT_CATEGORY element

4. Drag and Drop
This command is for drag and drop a web element and is rarely used.

Code for Drag and Drop:
/*   Drag And Drop

// Weblements for source and target
WebElement source = driver.findElement("source"));
WebElement target = driver.findElement("target"));

// Create object of actions class
Actions action = new Actions(driver);
action.dragAndDrop(source, target);;

Actions Class- Keyboard Events  << Previous   ||   Next >>   How to fetch Table data

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.


Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on using Selenium

Automate E-Commerce Website using Selenium Webdriver