πŸ“Œ Introduction

Selenium WebDriver provides the Actions class, which is designed to handle advanced user interactions such as mouse movements, keyboard events, drag-and-drop operations, and multiple actions in sequence. These interactions help in automating real-world user behaviors on web applications.

Many web elements require hovering, right-clicking, or drag-and-drop actions, which cannot be handled using simple click() or sendKeys(). The Actions class enables handling such scenarios effectively.


πŸ“Œ Why Use the Actions Class?

The Actions class is necessary when: ✔ Interacting with elements that require mouse movements (e.g., hovering over a dropdown menu).
Handling right-click, double-click, or drag-and-drop actions.
Performing keyboard interactions like pressing and holding keys (SHIFT, CTRL, ALT).
Simulating real user gestures for improved test automation accuracy.


πŸ“Œ How to Use the Actions Class?

Before using the Actions class, follow these steps:

1️⃣ Import the required Selenium package

import org.openqa.selenium.interactions.Actions;

2️⃣ Create an instance of the Actions class

Actions actions = new Actions(driver);

3️⃣ Call the required action method

actions.click().perform();

πŸ“Œ Methods of Actions Class in Selenium

The Actions class provides multiple methods for handling different types of interactions:

1️⃣ Mouse Actions

Mouse actions are used for hovering, clicking, dragging, and releasing elements.

Method
Description
click()
Clicks at the current mouse location.
click(WebElement element)
Clicks on a specific element.
doubleClick()
Double-clicks at the current mouse location.
doubleClick(WebElement element)
Double-clicks on a specific element.
contextClick()
Right-clicks at the current mouse location.
contextClick(WebElement element)
Right-clicks on a specific element.
moveToElement(WebElement element)
Moves the mouse pointer to an element (used for hover actions).
dragAndDrop(WebElement source, WebElement target)
Clicks and holds the source element, then drops it to the target element.
clickAndHold(WebElement element)
Clicks and holds the mouse button on an element.
release(WebElement element)
Releases the held mouse button.

2️⃣ Keyboard Actions

Keyboard actions simulate pressing, holding, and releasing keys.

Method
Description
sendKeys(Keys key)
Sends a keypress (e.g., ENTER, ESCAPE).
sendKeys(WebElement, Keys key)
Sends a keypress to a specific element.
keyDown(Keys key)
Presses and holds a key.
keyUp(Keys key)
Releases a pressed key.

Actions Class in Selenium

1️⃣ Hovering Over Elements

✅ Definition:

The moveToElement(WebElement element) method moves the mouse pointer to a specified element, simulating a hover effect.

If a regular .click() is not working due to hidden elements or dynamic behavior, use Actions class:

✅ Example: Hover Over a Menu

WebElement mainMenu = driver.findElement(By.id("mainMenu"));
WebElement subMenu = driver.findElement(By.id("subMenuOption"));

Actions actions = new Actions(driver);
actions.moveToElement(mainMenu).perform();  // Hover over the main menu
actions.moveToElement(subMenu).click().perform();  // Click the submenu 

✔ Use Case: Used for interacting with dropdown menus or tooltips that appear only on hover.

✔ It ensures that the mouse is properly positioned before clicking.

click(WebElement element)

✅ Definition:

The click(WebElement element) method performs a left-click on the specified WebElement. It is useful when normal .click() does not work due to JavaScript handling.

✅ Example: Clicking a Button

WebElement button = driver.findElement(By.id("submitButton"));

Actions actions = new Actions(driver);
actions.click(button).perform();

✔ Use Case: When .click() does not work due to hidden elements or JavaScript event handling.


2️⃣ Right-Click (Context Click) on a Specific Element

✅ Definition:

Some elements (e.g., dropdowns, context menus) require a right-click instead of a normal click. The contextClick(WebElement element) method performs a right-click on the specified WebElement.

✅ Example: Right-Click on an Element

WebElement menuElement = driver.findElement(By.id("rightClickMenu"));

Actions actions = new Actions(driver);
actions.contextClick(menuElement).perform();

✔ Use Case: Useful when testing context menus, dropdowns, or right-click functionalities.


3️⃣ Double-Click on an Element

✅ Definition:

Some buttons or text fields require a double-click action. The doubleClick() method performs a double-click at the current mouse pointer location.

✅ Example: Double-Click on a Text Box

WebElement textBox = driver.findElement(By.id("doubleClickInput"));

Actions actions = new Actions(driver);
actions.doubleClick(textBox).perform();

✔ Use Case: Used when a web element requires double-clicking to activate (e.g., renaming a file).


4️⃣ Drag and Drop Between Elements

✅ Definition:

The dragAndDrop(WebElement source, WebElement target) method drags an element from a source location and drops it at the target location.

✅ Example: Drag and Drop a File

WebElement sourceElement = driver.findElement(By.id("draggable"));
WebElement targetElement = driver.findElement(By.id("droppable"));

Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElement, targetElement).perform();

✔ Use Case: Used for sorting, file uploads, and interactive UI elements.

Alternative: Manual Drag and Drop Using Click and Hold

clickAndHold(WebElement element)

✅ Definition:

The clickAndHold(WebElement element) method clicks on an element and holds it without releasing.

✅ Example: Click and Hold an Element

WebElement draggable = driver.findElement(By.id("dragElement"));

Actions actions = new Actions(driver);
actions.clickAndHold(draggable).moveByOffset(100, 0).release().perform();


actions.clickAndHold(source)
       .moveToElement(target)
       .release()
       .perform(); 

✔ Use Case: Used when testing sliders, drag-and-drop elements, or signature fields.


5️⃣ Press and Hold a Key (SHIFT, CTRL, ALT)

✅ Definition:

Holding the SHIFT key while typing makes the text uppercase.

✅ Example: 

WebElement inputField = driver.findElement(By.id("textBox"));

Actions actions = new Actions(driver);
actions.keyDown(Keys.SHIFT).sendKeys(inputField, "selenium").keyUp(Keys.SHIFT).perform();

Expected Output: "SELENIUM" instead of "selenium"
Can also be used for keyboard shortcuts like CTRL + A, CTRL + C, etc.


6️⃣ Simulating Keyboard Shortcuts (CTRL + A, CTRL + C, CTRL + V)

✅ Definition:

Copy-Paste operation using Actions class:

✅ Example: 

WebElement inputField = driver.findElement(By.id("textBox"));

// Select all text (CTRL + A)
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

// Copy text (CTRL + C)
actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();

// Paste text (CTRL + V)
actions.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();

When to use this?
For testing keyboard shortcuts in web applications.


7️⃣ Moving the Mouse to an Offset Position

✅ Definition:

Sometimes, elements may not be clickable at their center due to overlays or hidden layers. You can click at an offset.

✅ Example: 

WebElement element = driver.findElement(By.id("targetElement"));

Actions actions = new Actions(driver);
actions.moveToElement(element, 10, 20).click().perform();

(10, 20) represents the offset in X and Y directions from the element’s top-left corner.


9️⃣ Clicking and Holding an Element for a Certain Time

✅ Example: 

WebElement button = driver.findElement(By.id("holdButton"));

Actions actions = new Actions(driver);
actions.clickAndHold(button).pause(Duration.ofSeconds(3)).release().perform();

When to use this?
For automating progress bars, long-press buttons, or hold-to-reveal options.

9️⃣ sendKeys(CharSequence keys)

✅ Definition:

The sendKeys(CharSequence keys) method simulates keyboard input.

✅ Example: Typing Text in an Input Field

WebElement inputField = driver.findElement(By.id("inputBox"));

Actions actions = new Actions(driver);
actions.sendKeys(inputField, "Selenium Actions Example").perform();

✔ Use Case: Useful when normal .sendKeys() does not work due to JavaScript-based inputs.


πŸ”Ÿ keyDown(Keys key) and keyUp(Keys key)

✅ Definition:

These methods simulate pressing and releasing keyboard keys.

✅ Example: Press and Release SHIFT Key

WebElement inputField = driver.findElement(By.id("inputBox"));

Actions actions = new Actions(driver);
actions.keyDown(Keys.SHIFT).sendKeys(inputField, "selenium").keyUp(Keys.SHIFT).perform();

✔ Use Case: Used for testing keyboard shortcuts, capital letters, and modifier keys.


πŸ“Œ Combining Multiple Actions (Action Chaining)

The Actions class allows multiple actions to be performed in sequence.

actions.moveToElement(menu).click()
       .keyDown(Keys.SHIFT).sendKeys("selenium").keyUp(Keys.SHIFT)
       .perform();

Here, hovering, clicking, and uppercase typing are done in one action.


πŸ“Œ Handling Tooltips Using Actions Class

Tooltips are usually displayed when hovering over an element. Use moveToElement() to capture tooltips.

WebElement tooltipElement = driver.findElement(By.id("tooltip"));
Actions actions = new Actions(driver);
actions.moveToElement(tooltipElement).perform();

πŸ“Œ Advanced Use Cases for Actions Class

πŸ’‘ Hover Over a Menu and Select a Sub-Option

Many websites have dropdown menus that only appear on hover. Use Actions class to handle this.

WebElement menu = driver.findElement(By.id("mainMenu"));
WebElement submenu = driver.findElement(By.id("subMenuOption"));

Actions actions = new Actions(driver);
actions.moveToElement(menu).perform(); // Hover over main menu
Thread.sleep(2000); // Wait for submenu to appear (Replace with Explicit Wait)
actions.moveToElement(submenu).click().perform(); // Click submenu option

Why is Thread.sleep() used here?
It is better to use Explicit Wait, but for quick testing, this helps when dealing with animation delays.


πŸ’‘ Performing a Mouse Scroll Action

If an element is not visible on the screen, you can scroll to it using JavaScript or Actions class.

Using JavaScript Executor:

WebElement element = driver.findElement(By.id("bottomElement"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);

Using Actions Class Scroll (For Selenium 4+)

actions.scrollToElement(element).perform();

When to use this?
For dealing with infinite scrolling pages, lazy-loaded content, or hidden elements.


πŸ“Œ Best Practices for Actions Class in Selenium

Use build().perform() when chaining multiple actions together.
Use moveToElement() before interacting with hidden dropdowns or tooltips.
Prefer scrollToElement() instead of JavaScript scrolling for better visibility control.
Use pause(Duration.ofSeconds(x)) when elements need a slight delay in hover actions.
Avoid Thread.sleep(), use Explicit Wait (WebDriverWait).


πŸ“Œ Conclusion

1. Always Use build().perform() for Multiple Actions

actions.moveToElement(element).click().build().perform();

2. Use pause(Duration.ofSeconds(x)) for Animations

Instead of Thread.sleep(), use:

actions.moveToElement(element).pause(Duration.ofSeconds(2)).click().perform();

3. Handle Hidden Elements with moveToElement()

actions.moveToElement(hiddenElement).click().perform();

4. Use clickAndHold() for Drag and Drop

actions.clickAndHold(source).moveToElement(target).release().perform();
  • The Actions class in Selenium enables advanced user interactions like mouse hover, right-click, double-click, drag-and-drop, and keyboard events.
  • It is particularly useful for dynamic web elements and JavaScript-driven applications.
  • It provides greater flexibility compared to basic WebDriver commands.