Skip to content

selenium_advanced_interactions

While basic commands like click() and sendKeys() cover many scenarios, modern web applications often require more complex user interactions such as mouse hovers, drag-and-drop, double-clicks, or keyboard shortcuts. Selenium WebDriver provides the Actions class to handle these advanced interactions.

The Actions class works by building a sequence of actions that are then performed in order. You must always call perform() at the end of the sequence to execute the actions.

First, you need to import the class and create an instance, passing the WebDriver object.

// Import the Actions class
// import org.openqa.selenium.interactions.Actions;
// Assuming 'driver' is your WebDriver instance
Actions actions = new Actions(driver);

Used to simulate moving the mouse cursor over an element, which might trigger a menu or tooltip to appear.

// Locate the main menu item
WebElement mainMenu = driver.findElement(By.id("main-menu-item"));
// Locate the submenu item that appears on hover
// (It might not be visible initially, ensure locator finds it once visible)
WebElement subMenu = driver.findElement(By.id("submenu-item"));
// Build the sequence: move to main menu, then move to submenu, then click
actions.moveToElement(mainMenu).moveToElement(subMenu).click().perform();
// Alternative: Move to main menu, wait for submenu, then click submenu
// actions.moveToElement(mainMenu).perform();
// WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
// WebElement visibleSubMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submenu-item")));
// actions.moveToElement(visibleSubMenu).click().perform(); // Or just visibleSubMenu.click();

Simulates clicking and holding an element (source) and moving it to another location (target) before releasing the mouse button.

// Locate the element to drag
WebElement sourceElement = driver.findElement(By.id("draggable"));
// Locate the target area to drop onto
WebElement targetElement = driver.findElement(By.id("droppable"));
// Option 1: Use the dedicated dragAndDrop method
actions.dragAndDrop(sourceElement, targetElement).perform();
// Option 2: Build the sequence manually
// actions.clickAndHold(sourceElement).moveToElement(targetElement).release().perform();

Simulates a right mouse click on an element, which usually opens a context menu.

// Locate the element to right-click
WebElement element = driver.findElement(By.id("context-menu-trigger"));
// Perform the right-click
actions.contextClick(element).perform();
// You would then need to locate and interact with elements in the context menu

Simulates double-clicking an element.

// Locate the element to double-click
WebElement element = driver.findElement(By.id("double-click-item"));
// Perform the double-click
actions.doubleClick(element).perform();

The Actions class can also send key presses, including modifier keys like Shift, Ctrl, Alt. Use the Keys enum (org.openqa.selenium.Keys).

// Locate an input field
WebElement inputField = driver.findElement(By.id("my-input"));
// Type text in uppercase using the Shift key
actions.moveToElement(inputField)
.click()
.keyDown(Keys.SHIFT) // Press Shift
.sendKeys("hello") // Send keys (will be uppercase)
.keyUp(Keys.SHIFT) // Release Shift
.sendKeys(" world") // Send more keys (normal case)
.perform();
// Example: Send Ctrl+A (Select All)
// actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
// Example: Send Enter key
// actions.sendKeys(Keys.ENTER).perform();

As seen in the examples, you can chain multiple actions together before calling perform(). This creates a single composite action that WebDriver executes.

WebElement element1 = driver.findElement(By.id("one"));
WebElement element2 = driver.findElement(By.id("two"));
// Example Chain: Move to element1, click and hold, move to element2, release
actions.moveToElement(element1)
.clickAndHold()
.moveToElement(element2)
.release()
.perform();
  • Always call perform(): The actions are only queued until perform() is called.
  • Element Visibility: Ensure elements are visible and interactable before attempting actions on them. Use Explicit Waits if necessary.
  • Browser Focus: Keyboard actions usually require the browser window or a specific element to have focus. Use click() or moveToElement().click() to ensure focus.
  • Complexity: While powerful, complex Actions sequences can sometimes be less stable than simpler interactions. Use them when standard commands are insufficient.

The Actions class significantly extends WebDriver’s capabilities, allowing you to automate virtually any user interaction found on modern web applications.