Bird
0
0

Given the code snippet:

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Given the code snippet:
driver.switchTo().frame(0);
String text = driver.findElement(By.id("insideFrame")).getText();
driver.switchTo().defaultContent();
String mainText = driver.findElement(By.id("outsideFrame")).getText();

What will text and mainText contain?
ACode will throw NoSuchElementException
BBoth will contain "Main Page Text"
C"Inside Frame Text" and "Main Page Text" respectively
DBoth will contain "Inside Frame Text"
Step-by-Step Solution
Solution:
  1. Step 1: Switching to iframe index 0

    The code switches focus to the first iframe (index 0), so findElement(By.id("insideFrame")) searches inside that iframe.
  2. Step 2: Switching back to main content

    After getting text inside iframe, defaultContent() switches focus back to main page, so findElement(By.id("outsideFrame")) searches main page.
  3. Final Answer:

    "Inside Frame Text" and "Main Page Text" respectively -> Option C
  4. Quick Check:

    Switch frame, get inside text; defaultContent, get main text [OK]
Quick Trick: Switch frame first, then defaultContent to access main page [OK]
Common Mistakes:
  • Trying to find iframe element without switching
  • Not switching back to main content before accessing main page elements
  • Assuming both texts come from same frame

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes