Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What is wrong with this code snippet?
driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
String text = driver.findElement(By.id("msg")).getText();

Assuming "frame2" is a sibling iframe of "frame1" on the main page.
ACode works fine
BCannot switch directly from one iframe to a sibling iframe without returning to main page
CSyntax error in frame switching
DNoSuchElementException because "msg" does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Understand iframe hierarchy

    "frame1" and "frame2" are siblings, so switching from one to the other directly is invalid.
  2. Step 2: Correct switching approach

    You must switch back to main page (defaultContent) before switching to a sibling iframe.
  3. Final Answer:

    Cannot switch directly from one iframe to a sibling iframe without returning to main page -> Option B
  4. Quick Check:

    Switch to sibling iframe requires defaultContent() first [OK]
Quick Trick: Use defaultContent() before switching to sibling iframe [OK]
Common Mistakes:
  • Trying to switch directly between sibling iframes
  • Ignoring iframe hierarchy
  • Assuming nested switching works for siblings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes