Working with windows and tabs
Windows and tabs
Get window handle
WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:
driver.getWindowHandle();
Switching windows or tabs
Clicking a link which opens in a new window will focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. If you have only two tabs or windows open, and you know which window you start with, by the process of elimination you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original. However, Selenium 4 provides a new api NewWindow which creates a new tab (or) new window and automatically switches to it.
                                
//Store the ID of the original window
String originalWindow = driver.getWindowHandle();
//Check we don't have other windows open already
assert driver.getWindowHandles().size() == 1;
//Click the link which opens in a new window
driver.findElement(By.linkText("new window")).click();
//Wait for the new window or tab
wait.until(numberOfWindowsToBe(2));
//Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
    if(!originalWindow.contentEquals(windowHandle)) {
        driver.switchTo().window(windowHandle);
        break;
    }
}
//Wait for the new tab to finish loading content
wait.until(titleIs("Selenium documentation"));
                            
                                
                              
                        Closing a window or tab
When you are finished with a window or tab and it is not the last window or tab open in your browser, you should close it and switch back to the window you were using previously. Assuming you followed the code sample in the previous section you will have the previous window handle stored in a variable. Put this together and you will get:
                                
//Close the tab or window
driver.close();
//Switch back to the old tab or window
driver.switchTo().window(originalWindow);
                            
                                
                              
                        Forgetting to switch back to another window handle after closing a window will leave WebDriver executing on the now closed page, and will trigger a No Such Window Exception. You must switch back to a valid window handle in order to continue execution.
Quitting the browser at the end of a session
When you are finished with the browser session you should call quit, instead of close:
                                driver.quit();
                              
                        
                                
Quit will:
Close all the windows and tabs associated with that WebDriver session
Close the browser process
Close the background driver process
Notify Selenium Grid that the browser is no longer in use so it can be used by another session (if you are using Selenium Grid)
Failure to call quit will leave extra background processes and ports running on your machine which could cause you problems later.
                                
                              
                        If not running WebDriver in a test context, you may consider using try / finally which is offered by most languages so that an exception will still clean up the WebDriver session.
                                
try {
    //WebDriver code here...
} finally {
    driver.quit();
}