Bird
0
0

What will be the output if the following code runs when an unexpected alert is present?

medium📝 Predict Output Q4 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will be the output if the following code runs when an unexpected alert is present?
try {
  driver.findElement(By.id("submit")).click();
} catch (UnhandledAlertException e) {
  driver.switchTo().alert().accept();
  System.out.println("Alert accepted");
}
AAlert accepted
BNo output, test fails silently
CUnhandledAlertException is not caught
DTest crashes with NoSuchElementException
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception handling

    The code catches UnhandledAlertException and accepts the alert.
  2. Step 2: Analyze output

    After accepting the alert, it prints "Alert accepted" to the console.
  3. Final Answer:

    Alert accepted -> Option A
  4. Quick Check:

    UnhandledAlertException caught and alert accepted = prints message [OK]
Quick Trick: Catch UnhandledAlertException to handle alerts [OK]
Common Mistakes:
MISTAKES
  • Assuming exception is not caught
  • Confusing exception types
  • Ignoring alert causes test failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes