Selenium + Java · Lesson 23 of 44
Module 23: Mouse & Keyboard Actions
Your task
Implement hoverAndClick:
Write it in the editor, then press Check answer (Ctrl + Enter).
Actions ClassAlways end action chains with .build().perform() or .perform():
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;
Actions actions = new Actions(driver);
// Hover to reveal submenu:
WebElement navMenu = driver.findElement(By.id("nav-products"));
actions.moveToElement(navMenu).perform();
// Context click (Right-click):
actions.contextClick(navMenu).perform();
// Drag and drop:
WebElement source = driver.findElement(By.id("draggable"));
WebElement target = driver.findElement(By.id("droppable"));
actions.dragAndDrop(source, target).perform();
// Modifier keys (Ctrl+A):
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();Complex interactions like hovering over navigation menus, right-clicking for context menus, and drag-and-drop require the Selenium Actions class. In this lesson, you will master chaining actions and calling build().perform(). Let's master the Actions class!
Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
You forgot to call .perform() at the end of the action chain! Without .perform(), the actions are only queued, not executed.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___