Bird
0
0

Identify the error in this code snippet handling unexpected alerts:

medium📝 Debug Q6 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Identify the error in this code snippet handling unexpected alerts:
try {
  driver.findElement(By.id("login")).click();
} catch (NoAlertPresentException e) {
  driver.switchTo().alert().accept();
}
ACatching NoAlertPresentException instead of UnhandledAlertException
BMissing driver.switchTo().frame() before alert
CUsing accept() instead of dismiss()
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception types

    UnhandledAlertException is thrown when alert blocks interaction, not NoAlertPresentException.
  2. Step 2: Identify mismatch in catch block

    The code catches wrong exception type, so alert handling will fail.
  3. Final Answer:

    Catching NoAlertPresentException instead of UnhandledAlertException -> Option A
  4. Quick Check:

    Catch correct alert exception = UnhandledAlertException [OK]
Quick Trick: Catch UnhandledAlertException for unexpected alerts [OK]
Common Mistakes:
  • Confusing NoAlertPresentException with UnhandledAlertException
  • Not switching to alert before accept()
  • Ignoring alert causes test failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes