Sunday, 23 October 2016

Handling Keyboard and Mouse Events with Selenium WebDriver

In many cases we will come across a scenario where we want to automate special type of Keyboard and Mouse events and for which Selenium Web Drover Provided Advance User Interface API with class names Actions and  Action.Handling keyboard events and mouse events in Selenium is very simple so let us start with it.

Let us see how we can use the Special Events in our Selenium Tests
Video Tutorial -

Test Scenario - Consider you want to perform mouse over action on the About Me menu as per given in below screenshot. As you observe the my site menu when you mouse over , it will change back ground color so lets see how we can test it using Selenium.

Test Case -

ackage mypackage;

import org.openqa.selenium.*;

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

public class myclass {

public static void main(String[] args) {
String baseUrl = "";
        WebDriver driver=new ChromeDriver();

        WebElement aboutMe= driver.findElement("aboout_me"));
        Actions builder = new Actions(driver);
        Action mouseOverAboutMe = builder
        String bColor = td_Home.getCssValue("background-color");
        System.out.println("Before Mouse hover: " + bColor );        
        bColor = td_Home.getCssValue("background-color");
        System.out.println("After Mouse hover: " + bColor );

Most Frequently Used  Keyboard and Mouse Events APIs -

1. clickAndHold() - Clicks without releasing at the pointed mouse location.

2. contextClick() -
Performs a context-click (Right Click) at the pointed mouse location.

3. doubleClick()-
Performs a double-click at the pointed mouse location.

4. dragAndDrop(source, target) -
Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
source- web element to emulate button down at.
target- web element to move to and release the mouse at.

5.dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
source- web element to emulate button down at.
xOffset- offcet ofhorizontal move.
yOffset- offset o fvertical move.

6.keyDown(modifier_key) -
Does not release the modifier key - subsequent interactions may assume it's kept pressed.
modifier key  sample =Keys.ALT, Keys.SHIFT, or Keys.CONTROL etc.

7.keyUp(modifier _key) -
Releases already pressed modified key.

8.moveByOffset(x-offset, y-offset) -
Moves the mouse from its current position by the given offset.
x-offset- horizontal offset. ( negative value =  moving the mouse left)
y-offset- vertical offset(negative value = moving the mouse up)

9. moveToElement(toElement) -
Moves the mouse to the middle of the element

10.release() -Releases the pressed left mouse button at the current mouse location pointer.

11.sendKeys(onElement, charsequence) -
Sends a series of keystrokes onto the element.

Please share this with your friends and leave your feedback in comment section.


  1. This comment has been removed by a blog administrator.


  2. Hello author,

    I found your blog while searching for the updates in Selenium Keyword,I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about selenium tutorials for beginners with reference of your blog.

  3. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    <a href="”> Selenium Training in Marathahalli </a>|

  4. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals. Selenium Training In Bangalore | Best Selenium Training in Bangalore