Hint: Always create Actions object with driver before contextClick() [OK]
Common Mistakes:
Calling contextClick() directly on driver
Using static call on Actions without new keyword
Missing perform() at the end
3. What will happen when the following Selenium Java code is executed?
WebElement element = driver.findElement(By.id("menu"));
new Actions(driver).contextClick(element).perform();
medium
A. A left-click is performed on the element with id 'menu'
B. Nothing happens because perform() is missing
C. An exception is thrown because contextClick() requires coordinates
D. A right-click is performed on the element with id 'menu'
Solution
Step 1: Locate the element by id 'menu'
The code finds the web element with id="menu" correctly using findElement(By.id("menu")).
Step 2: Perform contextClick() on the found element
The contextClick(element).perform() chain performs a right-click on that element.
Final Answer:
A right-click is performed on the element with id 'menu' -> Option D
Quick Check:
contextClick(element).perform() = right-click on element [OK]
Hint: contextClick(element).perform() triggers right-click on that element [OK]
Common Mistakes:
Thinking contextClick needs coordinates always
Confusing left-click with right-click
Forgetting to call perform()
4. Identify the error in the following Selenium Java code snippet that tries to right-click on a button:
WebElement button = driver.findElement(By.name("submit"));
Actions action = new Actions(driver);
action.contextClick().perform();
medium
A. contextClick() is called without specifying the element
B. Actions object is not created properly
C. perform() is missing after contextClick()
D. findElement locator is incorrect
Solution
Step 1: Check contextClick() usage
The method contextClick() 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 to contextClick(), so it will not right-click on the button.
Final Answer:
contextClick() is called without specifying the element -> Option A
Quick Check:
contextClick() needs element or mouse position [OK]
Hint: Always pass element to contextClick() to right-click on it [OK]
Common Mistakes:
Calling contextClick() without element or mouse position
Forgetting perform() after contextClick()
Assuming findElement locator is wrong without checking
5. You want to test a custom context menu that appears on right-clicking a list item. Which Selenium Java code snippet correctly performs a right-click on the first list item and then clicks the 'Delete' option from the context menu?