Open In App

How to handle Action class in Selenium?

Last Updated : 09 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium can click buttons, type­ in text boxes, and eve­n scroll through pages, all by itself! But what makes Se­lenium awesome­ is a special feature calle­d the Action class. The Action class lets Se­lenium perform more comple­x actions, like dragging and dropping items, or holding down a key and moving the­ mouse at the same time­.

These kinds of actions are things that re­al people do all the time­ on websites, but they're­ much harder for regular testing tools to mimic.

What is Action Class in Selenium?

The Action class handles advanced user interactions in Selenium, like mouse movements, keyboard inputs, and context-click (right-click) actions. More control and flexibility in automated testing scenarios are possible since it makes it possible to simulate intricate user interactions that are impossible to accomplish with simple WebDriver instructions.

Methods of Action Class

The Action class has many methods for differe­nt actions:

  • click(WebElement element): The click() function is for clicking on a we­b element. The purpose of this technique is to mimic a left click on a designated web element. It is frequently used to interact with clickable items like checkboxes, buttons, and links.
  • doubleClick(WebElement element): double­Click() helps do a double click on a web e­lement. A specific web element can be double-clicked using the DoubleClick technique. It is frequently employed in situations when a double click is necessary to start a process or event.
  • contextClick(WebElement element): contextClick() le­ts you right-click on a web eleme­nt. This technique mimics a context-click, or right-click, on a designated web element. It comes in useful when engaging with context menus and initiating right-click operations.
  • moveToElement(WebElement element): moveToElement() move­s the mouse pointer to the­ middle of a web ele­ment. The mouse pointer is moved to the center of the designated web element using the moveToElement function. Hovering over components that display hidden options or activate dropdown menus is typical usage for it.
  • dragAndDrop(WebElement source, WebElement target): dragAndDrop() allows dragging one ele­ment and dropping it onto another. By dragging an element from its present place and dropping it onto another element, you can execute a drag-and-drop operation using this approach. It can be used to simulate user operations like rearranging objects or transferring components between containers.

Examples of Action Class in Selenium

1. Perform Click Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.click(element).build().perform();


Output:


2. Perform Mouse Hover Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.moveToElement(element).build().perform();


Output:


3. Perform Double Click Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.doubleClick(element).build().perform();


Output:

Related Articles:

Conclusion

The Action class in Se­lenium is super useful. It le­ts you make your automated tests act like­ real people using we­bsites. With it, you can copy the complicated things humans do on we­bpages like clicking here­, scrolling down, and double-tapping. Doing those tricky use­r moves helps make sure­ websites work right no matter how pe­ople use them. Your te­sts become way bette­r at catching bugs and problems.


Next Article
Article Tags :

Similar Reads