Bird
0
0

Consider the following code snippet:

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Consider the following code snippet:
String mainWindow = driver.getWindowHandle();
Set allWindows = driver.getWindowHandles();
for (String window : allWindows) {
    if (!window.equals(mainWindow)) {
        driver.switchTo().window(window);
        driver.close();
    }
}
driver.switchTo().window(mainWindow);
System.out.println(driver.getTitle());
What will this code do?
AClose all windows including the main one and throw an error
BClose all windows except the main one and print the main window's title
CClose only the main window and print the last window's title
DSwitch to the last window and print its title without closing any window
Step-by-Step Solution
Solution:
  1. Step 1: Analyze window handles and loop

    The code saves the main window handle, loops through all windows, and closes any window not equal to mainWindow.
  2. Step 2: Switch back and print title

    After closing other windows, it switches back to mainWindow and prints its title.
  3. Final Answer:

    Close all windows except the main one and print the main window's title -> Option B
  4. Quick Check:

    Close others, keep main, print main title [OK]
Quick Trick: Close non-main windows, then switch back [OK]
Common Mistakes:
  • Closing main window inside the loop
  • Not switching back to main window before printing title
  • Assuming all windows are closed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes