πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 22 of 100
Mouse Actions
Advanced

Performing Double-Click on a Hidden Element

πŸ–±οΈScenario Overview

Performing Double-Click on a Hidden Element

Key Takeaways & Cheat Sheet

  • βœ“Confirm that Selenium Actions class requires elements to be visible in viewport
  • βœ“Verify that hidden elements (display:none) cannot receive standard mouse events
  • βœ“Modify element style to visible (display: block) via JavaScript Executor
  • βœ“Dispatch double-click events directly via Javascript Executor as a workaround

Short Direct Answer

Selenium's `Actions` class mimics physical mouse movements, which require the element to be visible and interactive in the viewport. If the element is hidden (e.g., hidden display styles), standard actions will fail. You must first alter the CSS properties via JavaScript to make the element visible, or trigger the double-click event directly via JavaScript.

⚠️ Senior Warning (Red Flag)

Do not attempt to use the Actions class on a hidden element. The Actions class simulates real mouse movements, and Selenium will throw an MoveTargetOutOfBoundsException.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Altering style properties via Javascript Executor is a highly effective way to automate interaction with hidden administrative elements or testing internal toggles.

SeleniumAutomation.java
Selenium 4 + Java
// βœ… Solution 1: Dispatch JS double-click directly (No visibility needed)
WebElement hiddenEl = driver.findElement(By.id("hidden-trigger"));
((JavascriptExecutor) driver).executeScript(
    "var evt = new MouseEvent('dblclick', {bubbles: true, cancelable: true}); arguments[0].dispatchEvent(evt);",
    hiddenEl
);

// βœ… Solution 2: Make element visible first, then use standard Actions
((JavascriptExecutor) driver).executeScript(
    "arguments[0].style.display='block'; arguments[0].style.visibility='visible';",
    hiddenEl
);
new Actions(driver).doubleClick(hiddenEl).perform();