Bird
0
0

You have multiple browser windows open. Write a code approach to close all windows except the main window using Selenium WebDriver in Java.

hard📝 Application Q8 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You have multiple browser windows open. Write a code approach to close all windows except the main window using Selenium WebDriver in Java.
ASwitch to main window and call driver.close() only
BCall driver.close() repeatedly without switching windows
CGet all window handles, loop through them, switch to each except main, then close
DUse driver.quit() to close all windows at once
Step-by-Step Solution
Solution:
  1. Step 1: Retrieve all window handles

    Use getWindowHandles() to get all open windows.
  2. Step 2: Loop and close non-main windows

    Loop through handles, switch to each window except the main one, and call close() to close it.
  3. Final Answer:

    Get all window handles, loop through them, switch to each except main, then close -> Option C
  4. Quick Check:

    Close all except main by switching and closing [OK]
Quick Trick: Switch to each window except main, then close it [OK]
Common Mistakes:
MISTAKES
  • Calling close() without switching windows
  • Using quit() which closes all windows
  • Closing main window instead of others

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes