Handling keyboard events and mouse hover events using Webdriver

In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.
In order to perform action events, we need to use org.openqa.selenium.interactions.Actions class.

Here is the sample code to work with Action Class

// Configure the Action
Actions action = new Actions(driver);

// To click on the element
action.moveToElement(element).click().perform();

Note: We need to use perform() to execute the action.

Using Action API, keyboard interactions are simple to do with webdriver. In Advanced User Interactions API, interaction with element is possible either by clicking on element or sending a Keys using sendKeys()
Check out the example

To use mouse actions, we need to use current location of the element and then perform the action.

The following are the regularly used mouse and keyboard events :

Method :clickAndHold()
Purpose: Clicks without releasing the current mouse location

Method : contextClick()
Purpose: Performs a context-click at the current mouse location.
How to work with context menu by taking a simple example

Method: doubleClick()
Purpose: Performs a double click at the current mouse location

Method: dragAndDrop(source,target)
Parameters: Source and Target
Purpose: Performs click and hold at the location of the source element and moves to the location of the target element then releases the mouse.

Click here for Example

Method : dragAndDropBy(source,x-offset,y-offset)
Parameters: Source, xOffset - horizontal move, y-Offset - vertical move Offset
Purpose: Performs click and hold at the location of the source element moves by a given off set, then releases the mouse.

Method: keyDown(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a modifier key press, doesn't release the modifier key. Subsequent interactions may assume it's kept pressed

Method: keyUp(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a key release.

Method: moveByOffset(x-offset, y-offset)
Parameters: X-Offset , Horizontal offset, a negative value means moving the mouse to left side.
Y-Offset, vertical offset, a negative value means moving the mouse to up.
Purpose: Moves the mouse position from its current position by the given offset.

Check for the example here Resizing a web element using movebyoffset

Method: moveToElement(toElement)
Parameters: toElement - Element to Move to
Purpose: It moves the Mouse to the middle of the element.

Method: release()
Purpose: It releases the left mouse button at the current mouse location.

Method: sendKeys(onElement, charSequence)
Parameters: onElement, which will receive the keyStrokes, usually text field.
charsequence- any string value representing the sequence of keyStrokes to be sent.
Purpose: It sends a series of keyStrokes onto the element

Selenium Tutorials: 

Comments

I need more information regadring Action class and Keybord Event ,mouse Event using java selenium code

I feel very confident after reading/practicing your examples. Its great. Thank you so much. Have you posted a link of interview questions anywhere? Please tell me...

This is very much help. Thanks for the your effort making the things more understandable with examples.

This was very helpful. Thanks a lot.

Dear sir/madam In the web page Handling keyboard events and mouse hover events using Webdriver the method CONTEXTCLICK() is typed as CONTENTCLICK(), you can edit it..........>)

Thank You.

Corrected.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.