Bird
0
0

Consider this code:

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Consider this code:
driver.switchTo().window("popupWindow");
WebElement closeBtn = driver.findElement(By.id("close"));
closeBtn.click();
driver.switchTo().defaultContent();

What will happen when this code runs?
AClicks close button in popup window, then throws exception on defaultContent()
BClicks close button in popup window, then switches to default content of the popup window
CThrows NoSuchWindowException at switchTo().window()
DClicks close button in main window
Step-by-Step Solution
Solution:
  1. Step 1: Switch to popup window by name

    driver.switchTo().window("popupWindow") changes context to popup window, so close button is found and clicked.
  2. Step 2: Switch to default content of current window

    defaultContent() switches to the top frame (default content) of the current popup window.
  3. Final Answer:

    Clicks close button in popup window, then switches to default content of the popup window -> Option B
  4. Quick Check:

    Window switch then defaultContent affects current window frames [OK]
Quick Trick: Use window() to switch windows, defaultContent() to top frame of current window [OK]
Common Mistakes:
MISTAKES
  • Expecting defaultContent() to throw exception after window switch
  • Confusing window() with frame()
  • Assuming close button is in main window

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes