A. Close all windows including the main one and throw an error
B. Close all windows except the main one and print the main window's title
C. Close only the main window and print the last window's title
D. Switch to the last window and print its title without closing any window
Solution
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.
Step 2: Switch back and print title
After closing other windows, it switches back to mainWindow and prints its title.
Final Answer:
Close all windows except the main one and print the main window's title -> Option B
Quick Check:
Close others, keep main, print main title [OK]
Hint: 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
4. Identify the error in this Selenium Java code snippet for switching to a new window:
Set<String> windows = driver.getWindowHandles();
for (String win : windows) {
if (win != driver.getWindowHandle()) {
driver.switchTo().window(win);
break;
}
}
medium
A. Using '!=' to compare strings instead of .equals()
B. Missing driver.quit() after switching windows
C. Not closing the previous window before switching
D. Using getWindowHandles() instead of getWindowHandle()
Solution
Step 1: Check string comparison method
In Java, strings must be compared with .equals(), not '!=' which compares references.
Step 2: Identify impact of wrong comparison
Using '!=' may cause the condition to fail unexpectedly, so the switch may not happen correctly.
Final Answer:
Using '!=' to compare strings instead of .equals() -> Option A
Quick Check:
Use .equals() for string comparison [OK]
Hint: Always use .equals() to compare strings in Java [OK]
Common Mistakes:
Using '==' or '!=' for string comparison
Forgetting to break after switching
Confusing getWindowHandle() and getWindowHandles()
5. You have a test that opens multiple tabs. You want to switch to the tab with title "Dashboard" and perform actions there. Which approach correctly switches to that window?
hard
A. Close all windows except the first, then assume it is "Dashboard"
B. Use driver.switchTo().window("Dashboard"); directly with the title
C. Call driver.getWindowHandle() repeatedly until title is "Dashboard"
D. Iterate all window handles, switch to each, check title, break when title matches "Dashboard"
Solution
Step 1: Understand window switching by handle
You cannot switch windows by title directly; you must switch by handle and then check the title.
Step 2: Iterate handles and check titles
Loop through all window handles, switch to each, check if title equals "Dashboard", then break to stay on that window.
Final Answer:
Iterate all window handles, switch to each, check title, break when title matches "Dashboard" -> Option D
Quick Check:
Switch by handle, check title, then stop [OK]
Hint: Switch windows by handle, verify title, then stop [OK]
Common Mistakes:
Trying to switch using title as window handle
Not looping through all windows
Assuming first window is always the target
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.