Bird
0
0

You want to write a method that safely handles unexpected alerts during any WebDriver action. Which approach is best?

hard📝 Application Q8 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You want to write a method that safely handles unexpected alerts during any WebDriver action. Which approach is best?
AIgnore alerts and let tests fail if alert appears
BUse Thread.sleep() before every action to wait for alerts to disappear
CWrap actions in try-catch for UnhandledAlertException, switch to alert, accept it, then retry action
DClose browser and restart WebDriver on alert detection
Step-by-Step Solution
Solution:
  1. Step 1: Handle unexpected alerts gracefully

    Wrap WebDriver actions in try-catch to catch UnhandledAlertException.
  2. Step 2: Accept alert and retry action

    Switch to alert, accept it, then retry the original action to continue test smoothly.
  3. Final Answer:

    Wrap actions in try-catch, accept alert, retry action -> Option C
  4. Quick Check:

    Try-catch + accept alert + retry = robust alert handling [OK]
Quick Trick: Catch alert exception, accept alert, retry action [OK]
Common Mistakes:
MISTAKES
  • Using Thread.sleep() wastes time
  • Ignoring alerts causes test failures
  • Restarting browser loses test state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes