Bird
0
0

How can you combine getWindowHandles() with a loop to close all windows except the main one?

hard📝 Application Q9 of 15
Selenium Java - Handling Windows, Frames, and Alerts
How can you combine getWindowHandles() with a loop to close all windows except the main one?
ACall driver.close() once without switching windows
BIterate all handles, switch to each, close if not main handle
CUse getWindowHandle() to get all handles and close them
DClose windows by their titles without switching
Step-by-Step Solution
Solution:
  1. Step 1: Get main window handle

    Use getWindowHandle() to store the main window's handle.
  2. Step 2: Iterate over all window handles

    Use getWindowHandles() to get all handles and loop through them.
  3. Step 3: Switch and close windows except main

    Switch to each handle and close it if it is not the main window.
  4. Final Answer:

    Iterate all handles, switch to each, close if not main handle -> Option B
  5. Quick Check:

    Switch and close non-main windows in loop [OK]
Quick Trick: Switch to each window and close if not main [OK]
Common Mistakes:
MISTAKES
  • Closing windows without switching
  • Using getWindowHandle() to get all handles
  • Closing windows by title without switching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes