Bird
0
0

You want to write a Selenium Java test that accepts a confirmation alert only if its text contains the word 'Proceed'. Which code snippet correctly implements this?

hard📝 Application Q8 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You want to write a Selenium Java test that accepts a confirmation alert only if its text contains the word 'Proceed'. Which code snippet correctly implements this?
AAlert alert = driver.switchTo().alert(); alert.accept(); if(alert.getText().contains("Proceed")) { alert.dismiss(); }
BAlert alert = driver.switchTo().alert(); if(alert.getText().contains("Proceed")) { alert.accept(); } else { alert.dismiss(); }
Cdriver.switchTo().alert().accept(); if(driver.switchTo().alert().getText().contains("Proceed")) { driver.switchTo().alert().dismiss(); }
DAlert alert = driver.switchTo().alert(); alert.dismiss(); alert.accept();
Step-by-Step Solution
Solution:
  1. Step 1: Switch to alert and get text

    Use alert.getText() to check alert message.
  2. Step 2: Conditionally accept or dismiss alert

    If text contains 'Proceed', accept; else dismiss.
  3. Final Answer:

    Alert alert = driver.switchTo().alert(); if(alert.getText().contains("Proceed")) { alert.accept(); } else { alert.dismiss(); } -> Option B
  4. Quick Check:

    Check text before accept/dismiss [OK]
Quick Trick: Check alert text before accept or dismiss [OK]
Common Mistakes:
  • Calling accept() before checking text
  • Calling getText() after accept() or dismiss()
  • Switching to alert multiple times unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes