Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What is wrong with this code?
driver.switchTo().alert().accept();
driver.switchTo().frame("frame2");
WebElement btn = driver.findElement(By.id("btn"));
btn.click();
ANo switch back to default content before switching to frame
BAlert accept() should be after frame switch
CNo error, code is correct
Dframe() method cannot accept string
Step-by-Step Solution
Solution:
  1. Step 1: Dismiss the alert

    driver.switchTo().alert().accept() handles the alert and returns driver context to the page automatically.
  2. Step 2: Switch to frame directly

    Context is now page-level, so driver.switchTo().frame("frame2") works; btn is found and clicked.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    Alert accept returns to page context, frame switch OK [OK]
Quick Trick: No defaultContent() needed after alert().accept() [OK]
Common Mistakes:
  • Thinking defaultContent() is required after alert accept
  • Thinking frame() can't take string
  • Misordering alert and frame switches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes