Bird
0
0

You want to test a prompt alert that asks for a username. Which Selenium Java code snippet correctly enters "user123" and confirms the alert?

hard📝 Application Q15 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You want to test a prompt alert that asks for a username. Which Selenium Java code snippet correctly enters "user123" and confirms the alert?
Adriver.switchTo().alert().sendKeys("user123"); driver.switchTo().alert().accept();
BAlert alert = driver.switchTo().alert(); alert.dismiss(); alert.sendKeys("user123");
CAlert alert = driver.switchTo().alert(); alert.sendKeys("user123"); alert.accept();
Ddriver.switchTo().alert().accept(); driver.switchTo().alert().sendKeys("user123");
Step-by-Step Solution
Solution:
  1. Step 1: Understand alert handling best practice

    Store the alert in a variable to reuse it for sendKeys() and accept().
  2. Step 2: Check order of operations

    First sendKeys() to enter text, then accept() to confirm and close alert.
  3. Final Answer:

    Alert alert = driver.switchTo().alert(); alert.sendKeys("user123"); alert.accept(); -> Option C
  4. Quick Check:

    Store alert, sendKeys, then accept [OK]
Quick Trick: Assign alert to variable before sendKeys and accept [OK]
Common Mistakes:
  • Calling accept() before sendKeys()
  • Using dismiss() which cancels input
  • Calling switchTo().alert() twice unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes