What if you could switch between browser windows like flipping pages in a book, without losing track?
Why Window handles (getWindowHandles) in Selenium Java? - Purpose & Use Cases
Imagine testing a website that opens multiple pop-up windows or tabs. You try to switch between them manually by clicking and checking each one to see if it has the content you want.
This manual way is slow and confusing. You might lose track of which window is which. It's easy to click the wrong window or miss testing some windows entirely. This wastes time and causes mistakes.
Using window handles in Selenium lets you get a list of all open windows automatically. You can switch between them by their unique IDs, making your tests fast, clear, and reliable.
driver.switchTo().window("windowName"); // guessing window name manuallySet<String> handles = driver.getWindowHandles();
for(String handle : handles) {
driver.switchTo().window(handle);
// test window content
}This lets you control and test multiple windows or tabs easily, just like flipping pages in a book without losing your place.
Testing a login flow where clicking 'Help' opens a new window. You can switch to that window, check the help content, then return to the main window smoothly.
Manual window switching is confusing and error-prone.
Window handles give unique IDs for all open windows.
Using getWindowHandles() makes multi-window testing simple and reliable.