Bird
0
0

What will happen when the following code executes?

medium📝 Predict Output Q4 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will happen when the following code executes?
driver.switchTo().frame(1);
String text = driver.findElement(By.id("title")).getText();
System.out.println(text);

Assuming the page has two iframes and the second iframe contains an element with id "title" having text "Welcome".
AThrows NoSuchElementException
BPrints "Welcome" to the console
CThrows NoSuchFrameException
DPrints empty string
Step-by-Step Solution
Solution:
  1. Step 1: Switch to second iframe by index

    Index 1 refers to the second iframe on the page, so driver switches focus correctly.
  2. Step 2: Find element with id "title" inside iframe

    The element exists and contains text "Welcome", so getText() returns "Welcome".
  3. Final Answer:

    Prints "Welcome" to the console -> Option B
  4. Quick Check:

    Switching by index works; element found inside iframe [OK]
Quick Trick: Index 1 means second iframe; element found inside prints text [OK]
Common Mistakes:
  • Assuming index starts at 1 instead of 0
  • Not switching to iframe before finding element
  • Confusing NoSuchFrameException with NoSuchElementException

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes