Keyboard And Mouse Events Using Selenium Actions Class

Selenium Actions Class:

Selenium has the built-in ability to handle various types of keyboard and mouse events. In order to do action events, you need to use org.openqa.selenium.interactions Actions class. The user-facing API for emulating complex user gestures. Use the selenium actions class rather than using the Keyboard or Mouse directly. This API includes actions such as drag and drop, clicking multiple elements.

To create an object ‘action‘ of Selenium Actions class:

To focus on element using WebDriver:

element is the webelement which we capture

perform() method is used here to execute the action.

To click on the element:

click() method is used here to click the element.

Methods Available in Selenium Actions Class:

Keyboard Events Using Selenium Actions Class API:

The Keyboard interface has the below mentioned methods:

  • sendKeys(keysToSend) : sends a series of keystrokes onto the element
  • keyDown(theKey) : Sends a key press without release it. Subsequent actions may assume it as pressed. (example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
  • keyUp(theKey): Performs a key release

Mouse Events Using Selenium Actions Class API:

  • click (): Simply click on element
  • doubleClick (): Double clicks onElement
  • contextClick() : Performs a context-click (right click) on an element
  • clickAndHold(): Clicks at the present mouse location (without releasing)
  • dragAndDrop(source, target): Invokes click-and-hold at the source location and moves to the location of the target element before releasing the mouse. source – element to grab, target – element to release
  • dragAndDropBy(source, xOffset, yOffset) : Performs click-and-hold at the source location, shifts by a given offset, then frees the mouse. xOffset – to shift horizontally, yOffset – to shift vertically
  • moveByOffset(x-offset, y-offset): Shifts the mouse from its current position (or 0,0) by the given offset. x-offset – Sets the horizontal offset (negative value – shifting the mouse to the left), y-offset – Sets the vertical offset (negative value – shifting the mouse to the up)
  • moveToElement(toElement): It shifts the mouse to the center of the element
  • release(): Releases the depressed left mouse button at the existing mouse location

Some scenarios where we use Selenium Actions class are mentioned below. Check out the below links.

How to handle mouse hover actions using Actions in Selenium

How to do Drag and Drop using Actions in Selenium

How to Scroll Web Page Down or Up Using Selenium

How to Perform Context Click / Right Click using Actions in Selenium

How to Perform Double Click using Actions in Selenium

If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

Selenium Actions Class

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Leave a Comment

Share via
Copy link
Powered by Social Snap