Bird
0
0

Consider this code snippet:

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Consider this code snippet:
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println(alert.getText());

What will happen when this code runs?
ACode compiles but alert.getText() returns null
BAlert text is printed after accepting the alert
CAlert is dismissed and text is printed
DNo alert text printed, code throws NoAlertPresentException
Step-by-Step Solution
Solution:
  1. Step 1: Understand alert lifecycle after accept()

    accept() closes the alert, so alert no longer exists.
  2. Step 2: Calling getText() after accept()

    Calling getText() on closed alert throws NoAlertPresentException.
  3. Final Answer:

    No alert text printed, code throws NoAlertPresentException -> Option D
  4. Quick Check:

    getText() after accept() = Exception [OK]
Quick Trick: Don't getText() after accept(), alert is gone [OK]
Common Mistakes:
MISTAKES
  • Expecting alert text after accept()
  • Assuming alert still exists after accept()
  • Ignoring exception possibility

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes