Action Class in Selenium

Action class performs operations on keyboard and mouse events. Like refresh, double click, mouse over etc. WebElement cannot perform those operations.

Action class of selenium exists in the package ‘org.openqa.selenium.interactions’. Actions given in below table are most commonly used functions of Action class.



Method Name Description
addAction Adds an action to current list of actions to be performed.
build Builds the sequence of actions.
click() Clicks the mouse at the last known mouse coordinates.
click(IWebElement) Clicks the mouse on the specified element.
clickAndHold() Clicks and holds the mouse button at the last known mouse coordinates.
clickAndHold(IWebElement) Clicks and holds the mouse button down on the specified element.
contextClick() Right-clicks the mouse at the last known mouse coordinates.
contextClick(IWebElement) Right-clicks the mouse on the specified element.
doubleClick() Double-clicks the mouse at the last known mouse coordinates.
doubleClick(IWebElement) Double-clicks the mouse on the specified element.
dragAndDrop Performs a drag-and-drop operation from one element to another.
dragAndDropToOffset Performs a drag-and-drop operation on one element to a specified offset.
equals Determines whether the specified Object is equal to the current Object.(Inherited from Object.)
finalize Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.(Inherited from Object.)
getHashCode Serves as a hash function for a particular type. (Inherited from Object.)
getLocatableFromElement Gets the ILocatable instance of the specified IWebElement. Gets the Type of the current instance.
getType (Inherited from Object.)
keyDown(String) Sends a modifier key down message to the browser.
keyDown(IWebElement, String) Sends a modifier key down message to the specified element in the browser.
keyUp(String) Sends a modifier key up message to the browser.
keyUp(IWebElement, String) Sends a modifier up down message to the specified element in the browser.
memberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
moveByOffset Moves the mouse to the specified offset of the last known mouse coordinates.
moveToElement(IWebElement) Moves the mouse to the specified element.
moveToElement(WebElement, Int32, Int32) Moves the mouse to the specified offset of the top-left corner of the specified element.
perform Performs the currently built action.
release() Releases the mouse button at the last known mouse coordinates.
release(IWebElement) Releases the mouse button on the specified element.
sendKeys(String) Sends a sequence of keystrokes to the browser.
sendKeys (IWebElement,String) Sends a sequence of keystrokes to the specified element in the browser.
to String Returns a String that represents the current Object. (Inherited from Object.)

Step 1: Import the Actions and Action classes

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

Step 2: Instantiate a new Actions object.

    Actions builder = new Actions(driver);

Step 3: Instantiate an Action using the Actions object in step 2.

    Action mouseOverHome = builder.moveToElement(link_Home).build();

In this case, we are going to use the moveToElement() method because we are simply going to mouse-over the "Home" link. The build () method is always the final method used so that all the listed actions will be compiled into a single step.

Step 4: Use perform() method when executing the Action object we designed in Step 3.

    mouseOverHome.perform();

You can build a series of multiple actions using the Action and Actions classes. Just remember to close the series with the build () method.


action class example