Skip to main content

You are here

How to simulate mouse movement in Selenium WebDriver

This code navigates to a website It then simulates moving the mouse from one menu link to the other. Observe the dropdown menu as focus shifts from one menu option to the other. There is a slight delay between each menu option (curtesy of Thread.sleep).

For some strange reason a previously visited menu link's dropdown remained even after focus shifted. Hoping some gurus can explain that to us in the comments sections. smiley

Note: I have assumed you already know how to configure Selenium WebDriver on Eclispe IDE (where I tested the following codes). Code require Selenium Library to run.

import java.util.concurrent.TimeUnit;
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 MouseMover {

public static void main(String[] args) throws InterruptedException {
//Initialise firefox browser
WebDriver driver = new FirefoxDriver();

/* Next line makes the browser wait for 7 seconds before declaring it cant find an element.
Good for slow loading websites*/

//Build webElement object to contain menu link xpath for all the mainmenu links
WebElement HomeLink = driver.findElement(By.xpath("//*[@id='globalNav']/ul/li[1]/a"));
WebElement ShowsLink = driver.findElement(By.xpath("//*[@id='globalNav']/ul/li[2]/a"));  
WebElement MusicLink = driver.findElement(By.xpath("//*[@id='globalNav']/ul/li[3]/a"));
WebElement EventsLink = driver.findElement(By.xpath("//*[@id='globalNav']/ul/li[6]/a"));

//Create an action object called myMouse
Actions myMouse = new Actions(driver);

//there is a slight delay before each mouse movement hence the "Thread.sleep" statement
myMouse.moveToElement(ShowsLink).build().perform();   //Shows link
myMouse.moveToElement(HomeLink).build().perform();   //home link
myMouse.moveToElement(MusicLink).build().perform();   //Music link
myMouse.moveToElement(EventsLink ).build().perform();   //Events link
myMouse.moveToElement(HomeLink).build().perform();   //Home link
myMouse.moveToElement(ShowsLink).build().perform();   //shows link


Here is the same operation done in c# but on another website: Observe the slight differences between Java and c#. Code structure is more or less the same but keywords have changed. For example WebDriver in Java is IWebDriver in c#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using NUnit.Framework;
using System.Threading;

namespace ConcepTestSuite
    class MainMenuPresent
        static void Main(string[] args)
            IWebDriver driver = new FirefoxDriver();
    Console.Write("Check if Music drop down is there in Main Navigation bar");

    //build and perform the mouseOver (Hover) with Advanced User Interactions API
    IWebElement HomeLink = driver.FindElement(By.XPath("//*[@id='home']"));
    IWebElement MusicLink = driver.FindElement(By.XPath("//*[@id='music']"));  
    IWebElement SportsLink = driver.FindElement(By.XPath("//*[@id='sports']"));
    IWebElement ArtsLink = driver.FindElement(By.XPath("//*[@id='arts']"));
    IWebElement FamilyLink = driver.FindElement(By.XPath("//*[@id='family']"));

            Actions builder  = new Actions(driver);//simply my webdriver

            builder.MoveToElement(HomeLink).Build().Perform();   //home links
            builder.MoveToElement(SportsLink).Build().Perform();   //sports links
            builder.MoveToElement(FamilyLink).Build().Perform();   //family links
            builder.MoveToElement(MusicLink).Build().Perform();   //music links




No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.


Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes