Bird
0
0

Find the problem in this alert handling code:

medium📝 Debug Q7 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Find the problem in this alert handling code:
Alert alert = driver.switchTo().alert();
alert.accept();
String text = alert.getText();
AShould use dismiss() instead of accept()
BCalling getText() after accepting alert causes NoAlertPresentException
CMissing try-catch block for alert handling
DNo problem, code works fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand alert lifecycle

    Once alert is accepted, it no longer exists.
  2. Step 2: Calling getText() after accept()

    Calling getText() after accept() throws NoAlertPresentException because alert is gone.
  3. Final Answer:

    Calling getText() after accepting alert causes NoAlertPresentException -> Option B
  4. Quick Check:

    Get text before accept() to avoid exceptions [OK]
Quick Trick: Get alert text before accept() or dismiss() [OK]
Common Mistakes:
  • Accessing alert after it is closed
  • Not handling exceptions around alert
  • Confusing accept() and dismiss() effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes