Bird
0
0

Consider this code snippet:

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Consider this code snippet:
driver.switchTo().frame("frame1");
String header = driver.findElement(By.tagName("h1")).getText();
driver.switchTo().defaultContent();
String footer = driver.findElement(By.id("footer")).getText();
System.out.println(header + " - " + footer);

What is the expected output if the iframe "frame1" contains <h1>Hello</h1> and the main page contains <div id="footer">Bye</div>?
AHello - Bye
BHello - NoSuchElementException
CNoSuchElementException - Bye
DThrows NoSuchFrameException
Step-by-Step Solution
Solution:
  1. Step 1: Switch to iframe and get header text

    Switching to "frame1" allows finding <h1>Hello</h1> and storing "Hello".
  2. Step 2: Switch back to main page and get footer text

    Calling defaultContent() returns focus to main page, allowing finding footer with text "Bye".
  3. Final Answer:

    Hello - Bye -> Option A
  4. Quick Check:

    Switch to iframe, then defaultContent to main page [OK]
Quick Trick: Use defaultContent() to return to main page after iframe [OK]
Common Mistakes:
  • Trying to find main page elements without switching back
  • Confusing defaultContent() with parentFrame()
  • Assuming iframe elements are accessible without switching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes