Context click (right click) in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class ContextClickTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/context-menu-page"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement contextArea = wait.until(ExpectedConditions.elementToBeClickable(By.id("context-menu-area"))); Actions actions = new Actions(driver); actions.contextClick(contextArea).perform(); WebElement contextMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("context-menu"))); if (contextMenu.isDisplayed()) { System.out.println("Test Passed: Context menu is visible after right click."); } else { System.out.println("Test Failed: Context menu is not visible after right click."); } } finally { driver.quit(); } } }
This test script uses Selenium WebDriver with Java to automate the manual test case.
First, it opens the target web page.
Then it waits explicitly for the element with id 'context-menu-area' to be clickable.
It uses the Actions class to perform a context click (right click) on that element.
After the right click, it waits explicitly for the context menu with id 'context-menu' to become visible.
Finally, it asserts that the context menu is displayed and prints the test result.
The driver quits in a finally block to ensure the browser closes even if an error occurs.
Now add data-driven testing with 3 different elements having context menus to verify right click works on all.
Practice
contextClick() method in Selenium WebDriver do?Solution
Step 1: Understand the purpose of contextClick()
ThecontextClick()method is designed to simulate a right-click action on a web element, which is different from a normal click.Step 2: Compare with other mouse actions
Double-click, drag and drop, and typing are different actions and have their own methods likedoubleClick(),dragAndDrop(), andsendKeys().Final Answer:
Simulates a right-click on a web element -> Option BQuick Check:
contextClick() = right-click [OK]
- Confusing contextClick() with doubleClick()
- Using click() instead of contextClick() for right-click
- Trying to send keys for right-click action
Solution
Step 1: Identify the correct Actions class usage
In Selenium Java,Actionsis instantiated with the WebDriver instance, e.g.,new Actions(driver).Step 2: Use contextClick() on the Actions object and call perform()
The method chain isnew Actions(driver).contextClick(element).perform();to execute the right-click.Final Answer:
new Actions(driver).contextClick(element).perform(); -> Option CQuick Check:
Actions object + contextClick + perform() = correct syntax [OK]
- Calling contextClick() directly on driver
- Using static call on Actions without new keyword
- Missing perform() at the end
WebElement element = driver.findElement(By.id("menu"));
new Actions(driver).contextClick(element).perform();Solution
Step 1: Locate the element by id 'menu'
The code finds the web element withid="menu"correctly usingfindElement(By.id("menu")).Step 2: Perform contextClick() on the found element
ThecontextClick(element).perform()chain performs a right-click on that element.Final Answer:
A right-click is performed on the element with id 'menu' -> Option DQuick Check:
contextClick(element).perform() = right-click on element [OK]
- Thinking contextClick needs coordinates always
- Confusing left-click with right-click
- Forgetting to call perform()
WebElement button = driver.findElement(By.name("submit"));
Actions action = new Actions(driver);
action.contextClick().perform();Solution
Step 1: Check contextClick() usage
The methodcontextClick()requires either no argument (to right-click at current mouse location) or a WebElement argument to specify where to right-click. Here, no argument is passed but no mouse position is set.Step 2: Confirm if element is specified
The code finds the button element but does not pass it tocontextClick(), so it will not right-click on the button.Final Answer:
contextClick() is called without specifying the element -> Option AQuick Check:
contextClick() needs element or mouse position [OK]
- Calling contextClick() without element or mouse position
- Forgetting perform() after contextClick()
- Assuming findElement locator is wrong without checking
WebElement listItem = driver.findElement(By.cssSelector("ul#items li:first-child"));
WebElement deleteOption = driver.findElement(By.id("delete"));
// What next?Solution
Step 1: Right-click on the first list item
UsecontextClick(listItem).perform()to open the custom context menu on the list item.Step 2: Click the 'Delete' option separately
After the context menu appears, click the 'Delete' option usingdeleteOption.click();as it is a normal click on a visible element.Final Answer:
new Actions(driver).contextClick(listItem).perform(); deleteOption.click(); -> Option AQuick Check:
Right-click then normal click separately for context menu actions [OK]
- Chaining click on menu option inside Actions after contextClick
- Clicking wrong element first
- Not calling perform() after contextClick
Ready to prove it?
Pass the test and this pattern moves unknown
