Advanced User Interactions | Introducing Actions and moving the mouse over an element

In the Previous Tutorial, we learned to execute a piece of Javascript through WebDriver. In this tutorial, we’ll learn some advanced user interactions.

What is advanced user interactions?

Sometimes we need to interact with our system’s mouse or keyboard to simulate user interaction. For instance, let’s say, in a web application, there is a menu bar. On moving the mouse over one of the menu items, its sub-menu gets appear magically. We need to click on one of the sub-menu items.

Or let’s say we are on an e-commerce site looking for the latest smartphone. We need to drag and drop the price filter.  In these cases, we need our script to interact with the Systems’ mouse.

Similarly, we may encounter some scenarios where we are required to press certain keyboard buttons like TAB, Ctrl+C, Ctrl+VEsc, Enter or Function keys (f1, f2 etc). In this case, we would need our script to interact with the Systems’ keyboard.

WebDriver provides a class that encapsulate mechanism to achieve Advanced User Interactions. It has methods defined to perform specific tasks like moving mouse, right-click, pressing keyboard etc. It also has the mechanism to perform a set of tasks together. We’ll go through some of these methods in this as well as subsequent tutorials.

Moving mouse over an element

Let us take a scenario:

  • Go to 
  • On moving the mouse over ‘Movies, TV & Showtimes’ menu, a sub-menu gets to appear.
  • Under the submenu, click on the ‘Latest Trailers’ link. 

You need to import the following packages at the top of the file:

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

Instantiate Actions and pass a reference of WebDriver

Actions builder = new Actions(driver);

Call the method to move the mouse over the target element

Action myAction = builder

It looks like some typo. You were talking about Actions, not Action. what is the type of the myAction?

Well, the build() function generates a composite action that contains all actions to be performed on the element. It returns an object of type Action that is an interface. We need to call the perform() method to execute the action.


You can also combine the above two lines of codes into one:


First, we need to import the right package at the top of the file:

from selenium.webdriver import ActionChains

Instantiate ActionChains and pass a reference of WebDriver to it:

action_chains = ActionChains(driver)

Call the method to build the action for moving the mouse over the target element:


Perform the action:


When the above line of codes executes, it will perform the actions build by the builder. It’ll make the mouse hover over the target element, i.e ‘Movies, TV & Showtimes’. It’ll make the sub-menu appear so that we can click on the sub-menu item.

driver.findElement(By.linkText("In Theaters")).click();
driver.find_element_by_link_text("In Theaters").click()

Complete Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class AdvancedUserInteraction_MoveToElement {
 public static void main(String[] args) {
  System.setProperty("", "C:\\selenium\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  WebElement eleMenuShowtimes = driver.findElement("navTitleMenu"));
  Actions builder = new Actions(driver);
  Action myAction = builder
  //You can also combine above two line of codes into one
  driver.findElement(By.linkText("In Theaters")).click();
  if(driver.getTitle().contains("New Movies In Theaters"))
from selenium import webdriver
from selenium.webdriver import ActionChains

driver_path = "C:\\teachmeselenium\\chromedriver.exe"

driver = webdriver.Chrome(driver_path )
eleMenuShowtimes = driver.find_element_by_id("navTitleMenu")

action_chains = ActionChains(driver)
action_chains.move_to_element(eleMenuShowtimes)"In Theaters"))

if driver.title == "New Movies In Theaters":


We will explore some more options like drag-and-drop operations in the Next Tutorial.

Leave a Reply