Back to All Scenarios
Scenario 64 of 100
Element Interaction
Intermediate
Performing Right-Click Actions and Selecting Menus
π±οΈScenario Overview
Performing Right-Click Actions and Selecting Menus
Key Takeaways & Cheat Sheet
- βUtilize Selenium Actions class to perform complex context clicks
- βInvoke contextClick(element) to launch the mouse right-click menu
- βWait explicitly for options in the right-click menu to render
- βCall click() on the target menu element to complete selection
Short Direct Answer
To right-click, use the Selenium `Actions` class. Call `contextClick(element)` to open the context menu, wait explicitly for the menu options to load, and then click your target menu item.
β οΈ Senior Warning (Red Flag)
Never forget to call perform() at the end of Actions chains. Without perform(), the mouse actions are queued but never sent to the browser, leaving your script hanging.
π‘ STAR Deep Dive Explanation & Pro Tip
Always separate your hover/context actions from the click actions. Adding explicit waits between menu activation and selection prevent click intercepts.
SeleniumAutomation.java
Selenium 4 + Javaimport org.openqa.selenium.interactions.Actions;
WebElement targetEl = driver.findElement(By.id("product-card"));
Actions actions = new Actions(driver);
// β
1. Execute Right-Click (Context Click) and trigger menu options
actions.contextClick(targetEl).perform();
// β
2. Wait explicitly for context menu options visibility
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement deleteMenuOption = wait.until(
ExpectedConditions.elementToBeClickable(By.id("ctx-delete"))
);
// β
3. Select menu option
deleteMenuOption.click();