Bird
0
0

What will happen when the following code runs if an alert is present?

medium📝 Predict Output Q4 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will happen when the following code runs if an alert is present?
driver.switchTo().alert().dismiss();
System.out.println("Alert handled");
AAlert is dismissed but 'Alert handled' is not printed
BAlert is accepted and 'Alert handled' is printed
CNo alert action, code throws NoAlertPresentException
DAlert is dismissed and 'Alert handled' is printed
Step-by-Step Solution
Solution:
  1. Step 1: Understand dismiss() effect

    The dismiss() method closes the alert by clicking Cancel.
  2. Step 2: Check code flow after dismiss

    After dismissing, the print statement executes normally.
  3. Final Answer:

    Alert is dismissed and 'Alert handled' is printed -> Option D
  4. Quick Check:

    dismiss() closes alert, code continues [OK]
Quick Trick: dismiss() closes alert, code continues after [OK]
Common Mistakes:
MISTAKES
  • Thinking dismiss() accepts alert
  • Expecting exception if alert is present
  • Assuming print won't execute after dismiss

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes