Bird
0
0

Find the mistake in this code snippet that switches back to the main window:

medium📝 Debug Q7 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Find the mistake in this code snippet that switches back to the main window:
String mainWindow = driver.getWindowHandle();
// some code opens new window
driver.switchTo().window(mainWindow);
driver.close();
ANot switching to the new window before closing
BClosing the main window instead of the new window
CUsing getWindowHandle() instead of getWindowHandles()
DMissing driver.quit() after closing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze window switching and closing

    The code switches back to the main window and then closes it, which may close the main browser unexpectedly.
  2. Step 2: Identify intended behavior

    Usually, you want to close the new window, not the main window, so the order or window handle used is incorrect.
  3. Final Answer:

    Closing the main window instead of the new window -> Option B
  4. Quick Check:

    Close new window, not main window [OK]
Quick Trick: Close the new window handle, then switch back to main [OK]
Common Mistakes:
  • Closing main window accidentally
  • Not switching to new window before closing
  • Confusing getWindowHandle() with getWindowHandles()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes