top of page

Action Class in Selenium

Writer's picture: rathirengarajanrathirengarajan

Introduction

  • Actions class is a built-in ability to handle various types of keyboard and mouse events.

  • Selenium provides multiple options to automate it thereby reducing or completely wiping away the manual effort.

  • It includes various operations such as clicking on multiple elements with the help of control key, drag and drop events and many more are done using the advanced user interactions API.

The actions that can be performed in a browser are broadly classified into two categories namely-


1. Mouse actions

2. Keyboard actions

Mouse Actions

The various mouse actions that are provided by the Actions class are-

  1. click() – clicks at the current location

  2. doubleClick() – performs a Double click at the current mouse location

  3. contextClick() – performs a Right click at the current mouse location

  4. dragAndDrop(WebElement source, WebElement target) – drags an element from the source location and drops in target location

  5. dragAndDropBy(source, xOffset, yOffset) - Performs click-and-hold at the source location, shifts by a given offset value, then frees the mouse. (X offset – to shift horizontally, Y Offset – to shift vertically).

  6. moveToElement(WebElement target) – moves to the target element

Keyboard Actions

The various keyboard actions that are provided by the Actions class are-

  1. sendKeys (keysToSend) – Sends a series of keystrokes onto the element.

  2. keyDown – performs a key press without release.

(For Example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)

3. keyUp – performs a key release.

How to handle action class in selenium?

1. Import actions class


2. Create an object of the Actions class ‘action



3. We can use the different methods under the actions class to perform various operations like drag and drop, click and so on.






Few examples for the implementation of the action class method.

  • Drag and drop using source and target (Mouse actions)

code snippet:



Output for the above code






  • Drag and drop using source , x offset and y offset (Mouse actions)


code snippet:





Output for the above code





  • Converting Texts to Uppercase (Keyboard actions)


Code snippet:



Output for the above code






Hope you guys enjoyed reading this article and understood about the actions and the different methods under these actions class in Selenium.


174 views

Recent Posts

See All

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page