Managing Browser Windows, Tabs & Navigation
Why Multi-Window Handling Matters
Modern websites frequently open new tabs or pop-up windows - for example, when clicking a link that opens in a new tab, or a third-party login window. Selenium needs explicit instructions to know which window it should currently be controlling.
Switching Between Windows
Every browser window or tab Selenium opens is assigned a unique handle. By tracking these handles, scripts can switch focus between windows, perform actions in the new one, and return to the original window when finished.
- driver.window_handles - returns a list of all open window handles
- driver.switch_to.window() - switches control to a specific window
- driver.close() - closes only the currently focused window
- driver.quit() - closes the entire browser session, including all windows
Browser Navigation Actions
| Method | Action |
|---|---|
| driver.back() | Navigates to the previous page in browser history |
| driver.forward() | Navigates forward to the next page in history |
| driver.refresh() | Reloads the currently active page |
Common Pitfalls
A frequent mistake is trying to interact with an element in a new tab without first switching to it, which raises an error since Selenium is still focused on the original window. Always confirm the active handle before performing actions.
Ready to master Selenium Training Course?
Join Uncodemy's hands-on training and build real automation skills with expert mentors.
← Back to Course