Bird
0
0

Identify the error in this Selenium Java code snippet that tries to dismiss an alert:

medium📝 Debug Q14 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Identify the error in this Selenium Java code snippet that tries to dismiss an alert:
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
Aalert.dismiss() should be alert.cancel()
BCalling accept() before dismiss() causes an exception
CMissing driver.switchTo() before alert()
DCannot call dismiss() after accept() on the same alert instance
Step-by-Step Solution
Solution:
  1. Step 1: Understand alert lifecycle after accept()

    Once accept() is called, the alert is closed and no longer available.
  2. Step 2: Calling dismiss() after accept() is invalid

    Trying to dismiss the same alert after accepting it causes an error because alert is gone.
  3. Final Answer:

    Cannot call dismiss() after accept() on the same alert instance -> Option D
  4. Quick Check:

    accept() closes alert, no dismiss() after [OK]
Quick Trick: Alert closes after accept(), can't dismiss same alert [OK]
Common Mistakes:
MISTAKES
  • Calling dismiss() after accept() on same alert
  • Assuming alert stays open after accept()
  • Using non-existent cancel() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes