Selenium Actions Class

Introduction to Actions class

The Action class is used to perform Keyboard and Mouse actions in Selenium WebDriver. It is basically used to perform multiple actions in a sequence. The Actions class is used to perform complex user interactions like double-clicking, right-clicking, dragging, dropping, key up, key down etc.

Syntax to use Actions class

We will have to follow steps to use the Actions class in Selenium:
  • First, we have to find the element on which the action has to be performed using the findElement() method,
    WebElement element = driver.findElement(By.id("elementId"));
  • then create an object of the Actions class.
    Actions actions = new Actions(driver);
  • then sequence the actions using the actions object and finally call the perform() method to perform the actions.
    actions.moveToElement(element).click().perform();

Method Description
click() Clicks on the element
clickAndHold() Clicks on the element and holds the mouse button
contextClick() Performs a right-click on the element
doubleClick() Performs a double-click on the element
dragAndDrop() Drags the source element and drops it on the target element
keyDown() Performs a key down action on the keyboard
keyUp() Performs a key up action on the keyboard
moveByOffset() Moves the mouse from its current position by the given offset
moveToElement() Moves the mouse to the middle of the element
release() Releases the mouse button
Note: Just remember these methods and their usage. You can use these methods as per your requirement.