DEV Community

Saras Growth Space
Saras Growth Space

Posted on

Selenium Simplified — Mouse and Keyboard Actions in Selenium

Introduction

In the previous article, we explored how Selenium handles alerts, frames, and multiple browser windows by switching contexts.

Now let’s look at another important aspect of automation: advanced user interactions.

In real applications, users don’t just click buttons. They often perform actions like:

  • Hovering over menus
  • Dragging and dropping elements
  • Right-clicking to open context menus
  • Using keyboard shortcuts

Selenium provides the Actions class to simulate these complex interactions.


What is the Actions Class?

The Actions class in Selenium is used to perform advanced user interactions involving the mouse and keyboard.

It allows automation scripts to simulate behavior similar to how a real user interacts with the browser.

Example:

Actions actions = new Actions(driver);
Enter fullscreen mode Exit fullscreen mode

Once created, the Actions object can perform multiple interactions.


Hovering Over an Element

Many websites display menus only when the user hovers over an element.

For example:

  • Navigation menus
  • Dropdown menus
  • Tooltips

To simulate hovering, we use moveToElement().

Example

Actions actions = new Actions(driver);

WebElement menu = driver.findElement(By.id("menu"));
actions.moveToElement(menu).perform();
Enter fullscreen mode Exit fullscreen mode

This moves the mouse cursor over the element.


Drag and Drop

Some applications allow users to drag an element and drop it into another location.

Example scenarios:

  • Dragging items in dashboards
  • Reordering lists
  • Moving widgets

Example

Actions actions = new Actions(driver);

WebElement source = driver.findElement(By.id("item1"));
WebElement target = driver.findElement(By.id("dropArea"));

actions.dragAndDrop(source, target).perform();
Enter fullscreen mode Exit fullscreen mode

Here:

  • source is the element being dragged
  • target is where the element will be dropped

Right Click (Context Click)

Some applications display special menus when the user right-clicks on an element.

Selenium allows us to simulate this using contextClick().

Example

Actions actions = new Actions(driver);

WebElement element = driver.findElement(By.id("file"));
actions.contextClick(element).perform();
Enter fullscreen mode Exit fullscreen mode

This performs a right-click on the element.


Double Click

Some actions require a double click.

Example scenarios:

  • Opening files
  • Expanding sections
  • Activating UI components

Example

Actions actions = new Actions(driver);

WebElement button = driver.findElement(By.id("open"));
actions.doubleClick(button).perform();
Enter fullscreen mode Exit fullscreen mode

Keyboard Actions

The Actions class can also simulate keyboard interactions.

Examples include:

  • Pressing keys
  • Holding modifier keys
  • Typing combinations

Example: Sending Keyboard Keys

Actions actions = new Actions(driver);

actions.sendKeys(Keys.ENTER).perform();
Enter fullscreen mode Exit fullscreen mode

Example: Key Combinations

Example: Press CTRL + A to select all text.

actions.keyDown(Keys.CONTROL)
       .sendKeys("a")
       .keyUp(Keys.CONTROL)
       .perform();
Enter fullscreen mode Exit fullscreen mode

Action Builder Pattern

The Actions class allows multiple interactions to be chained together.

Example:

actions.moveToElement(menu)
       .click()
       .sendKeys("Test")
       .perform();
Enter fullscreen mode Exit fullscreen mode

Selenium performs the entire sequence as one action chain.


When to Use the Actions Class

You should use the Actions class when normal WebDriver commands are not enough.

Common cases include:

  • Hover menus
  • Drag-and-drop interfaces
  • Complex UI interactions
  • Keyboard shortcuts

Final Thoughts

Real user behavior involves more than just simple clicks.

Web applications often rely on hover effects, drag-and-drop actions, and keyboard interactions.

The Selenium Actions class allows automation scripts to simulate these behaviors and test complex UI functionality.

Understanding these interactions helps create more realistic and powerful automation tests.


Up Next in This Series

So far in this series we have covered:

  • Selenium architecture
  • Locators
  • Waits
  • Context switching
  • Advanced user interactions

Next, we will focus on validating test results.

In the next article:

Selenium Simplified — Assertions in Selenium

Assertions help verify that the application behaves as expected during testing.

Top comments (0)