Intermediate
Chapter 10 · 10 min read
Actions API — Advanced Interactions
Use the Actions class for drag-and-drop, hover, right-click, double-click, keyboard combos, and complex gesture chains.
Actions API — Complex Interactions
Basic click() and sendKeys() handle most interactions. But for hover menus, drag-and-drop, right-click context menus, and keyboard shortcuts, you need the Actions API. It builds chains of low-level actions and performs them atomically.
Common Actions
- moveToElement() — Hover over an element
- dragAndDrop() — Drag one element onto another
- contextClick() — Right-click to open context menu
- doubleClick() — Double-click an element
- keyDown/keyUp — Hold modifier keys (Ctrl, Shift, Alt)
- scrollToElement() — Selenium 4: scroll element into view
ActionsApiTest.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.*;
import java.time.Duration;
public class ActionsApiTest {
WebDriver driver;
Actions actions;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
actions = new Actions(driver);
}
@Test
public void testHover() {
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");
WebElement hoverable = driver.findElement(By.id("hover"));
actions.moveToElement(hoverable).perform();
// After hovering, check if sub-menu or tooltip appeared
System.out.println("Hovered over element");
}
@Test
public void testRightClick() {
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");
WebElement target = driver.findElement(By.id("clickable"));
actions.contextClick(target).perform();
}
@Test
public void testDoubleClick() {
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");
WebElement target = driver.findElement(By.id("clickable"));
actions.doubleClick(target).perform();
}
@Test
public void testDragAndDrop() {
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");
WebElement source = driver.findElement(By.id("draggable"));
WebElement target = driver.findElement(By.id("droppable"));
actions.dragAndDrop(source, target).perform();
}
@Test
public void testKeyboardShortcuts() {
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
WebElement input = driver.findElement(By.name("my-text"));
input.sendKeys("Hello World");
// Select all text (Ctrl+A) then copy (Ctrl+C)
actions.keyDown(Keys.CONTROL)
.sendKeys("a")
.keyUp(Keys.CONTROL)
.perform();
actions.keyDown(Keys.CONTROL)
.sendKeys("c")
.keyUp(Keys.CONTROL)
.perform();
}
@Test
public void testScrollToElement_Selenium4() {
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html");
WebElement target = driver.findElement(By.tagName("iframe"));
// Selenium 4: Scroll to element
actions.scrollToElement(target).perform();
}
@Test
public void testActionChaining() {
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
WebElement input = driver.findElement(By.name("my-text"));
// Chain multiple actions
actions.moveToElement(input)
.click()
.sendKeys("Chained actions")
.keyDown(Keys.SHIFT)
.sendKeys(" work!")
.keyUp(Keys.SHIFT)
.perform();
}
@AfterMethod
public void teardown() { if (driver != null) driver.quit(); }
}
test_actions_api.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import pytest
@pytest.fixture
def driver():
d = webdriver.Chrome()
d.implicitly_wait(10)
yield d
d.quit()
def test_hover(driver):
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html")
hoverable = driver.find_element(By.ID, "hover")
ActionChains(driver).move_to_element(hoverable).perform()
print("Hovered over element")
def test_right_click(driver):
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html")
target = driver.find_element(By.ID, "clickable")
ActionChains(driver).context_click(target).perform()
def test_double_click(driver):
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html")
target = driver.find_element(By.ID, "clickable")
ActionChains(driver).double_click(target).perform()
def test_drag_and_drop(driver):
driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html")
source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
ActionChains(driver).drag_and_drop(source, target).perform()
def test_keyboard_shortcuts(driver):
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
input_el = driver.find_element(By.NAME, "my-text")
input_el.send_keys("Hello World")
# Select all (Ctrl+A)
ActionChains(driver)\
.key_down(Keys.CONTROL)\
.send_keys("a")\
.key_up(Keys.CONTROL)\
.perform()
def test_scroll_to_element_selenium4(driver):
"""Selenium 4 scroll to element"""
driver.get("https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
target = driver.find_element(By.TAG_NAME, "iframe")
ActionChains(driver).scroll_to_element(target).perform()
def test_action_chaining(driver):
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
input_el = driver.find_element(By.NAME, "my-text")
(ActionChains(driver)
.move_to_element(input_el)
.click()
.send_keys("Chained actions")
.key_down(Keys.SHIFT)
.send_keys(" work!")
.key_up(Keys.SHIFT)
.perform())
Selenium
Intermediate
Actions API — Advanced Interactions
Written by PV
© 2026 All Rights Reserved