Bird
0
0

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?

hard📝 Application Q15 of 15
Selenium Java - Actions Class
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?
WebElement listItem = driver.findElement(By.cssSelector("ul#items li:first-child"));
WebElement deleteOption = driver.findElement(By.id("delete"));
// What next?
Anew Actions(driver).contextClick(listItem).perform(); deleteOption.click();
Bnew Actions(driver).click(listItem).contextClick(deleteOption).perform();
Cnew Actions(driver).contextClick(listItem).click(deleteOption).perform();
Dnew Actions(driver).contextClick(deleteOption).click(listItem).perform();
Step-by-Step Solution
Solution:
  1. Step 1: Right-click on the first list item

    Use contextClick(listItem).perform() to open the custom context menu on the list item.
  2. Step 2: Click the 'Delete' option separately

    After the context menu appears, click the 'Delete' option using deleteOption.click(); as it is a normal click on a visible element.
  3. Final Answer:

    new Actions(driver).contextClick(listItem).perform(); deleteOption.click(); -> Option A
  4. Quick Check:

    Right-click then normal click separately for context menu actions [OK]
Quick Trick: Right-click then click menu option separately for context menus [OK]
Common Mistakes:
MISTAKES
  • Chaining click on menu option inside Actions after contextClick
  • Clicking wrong element first
  • Not calling perform() after contextClick

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes