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();
String alertText = alert.getText();
alert.dismiss();
System.out.println(alertText);

What will this code do when an alert is present?
ACloses alert without printing text
BThrows NoAlertPresentException
CPrints alert text but does not close alert
DPrints alert text and closes the alert
Step-by-Step Solution
Solution:
  1. Step 1: Switch to alert and get text

    The code switches to alert and stores its text in alertText.
  2. Step 2: Dismiss alert and print text

    Calling dismiss() closes the alert, then alertText is printed.
  3. Final Answer:

    Prints alert text and closes the alert -> Option D
  4. Quick Check:

    Get text + dismiss alert = print text and close alert [OK]
Quick Trick: Use getText() before dismiss() to read alert message [OK]
Common Mistakes:
MISTAKES
  • Calling getText() after dismiss() causes error
  • Assuming dismiss() does not close alert
  • Not switching to alert before getText()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes