Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will be the output of the following code snippet?
driver.switchTo().frame("frame1");
String text = driver.findElement(By.id("msg")).getText();
driver.switchTo().defaultContent();
System.out.println(text);

Assuming the element with id msg inside frame1 contains text "Hello Frame".
AHello Frame
BNoSuchElementException
Cnull
DStaleElementReferenceException
Step-by-Step Solution
Solution:
  1. Step 1: Switch to iframe and get text

    The code switches to iframe "frame1" and finds element with id "msg" inside it, getting its text "Hello Frame".
  2. Step 2: Switch back to main content and print text

    After switching back to main content, the stored text variable still holds "Hello Frame" and prints it.
  3. Final Answer:

    Hello Frame -> Option A
  4. Quick Check:

    Switch frame, get text, print text = "Hello Frame" [OK]
Quick Trick: Switch before findElement; text stays after switching back [OK]
Common Mistakes:
MISTAKES
  • Trying to find element without switching to iframe
  • Expecting error after switching back
  • Confusing element scope after context switch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes