Introduction
While automating web applications, Selenium often opens multiple browser windows or tabs. After completing the required actions in a secondary window, it is important to close only that window and continue working in the original window.
Selenium provides the close() method to close the currently active browser window, while quit() closes all open browser windows and ends the WebDriver session.
In this tutorial, you’ll learn how to close browser windows using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is Window Closing?
Window closing is the process of closing the currently active browser window while leaving other open windows available.
For example:
Original Window
│
▼
Open New Window
│
▼
Close New Window
│
▼
Continue in Original Window
This is commonly used when a child window is no longer needed.
close() vs quit()
| Method | Description |
|---|---|
driver.close() | Closes only the currently active browser window. |
driver.quit() | Closes all browser windows and ends the WebDriver session. |
Why Close Windows?
Closing browser windows helps you:
Return to the main application.
Clean up unnecessary browser windows.
Continue testing without interference.
Simulate real user behavior.
Improve test stability.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# Topic: 30. Windows and Tabs - Closing Windows
# Practice site: https://the-internet.herokuapp.com/windows
# Run: pytest -s 30_examples/test_04_closing_windows.py
#
# driver.close() closes only the current window. After closing a child window,
# switch back to the original window before continuing.
def test_close_child_window_and_return_to_original():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/windows")
original_window = driver.current_window_handle
driver.find_element(By.LINK_TEXT, "Click Here").click()
WebDriverWait(driver, 10).until(lambda browser: len(browser.window_handles) == 2)
new_window = [handle for handle in driver.window_handles if handle != original_window][0]
driver.switch_to.window(new_window)
driver.close()
driver.switch_to.window(original_window)
assert len(driver.window_handles) == 1
assert driver.find_element(By.TAG_NAME, "h3").text == "Opening a new window"
finally:
driver.quit()
Understanding the Code
Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
These modules are required to launch the browser, locate web elements, and wait for the second browser window to open.
Create a Chrome Browser Instance
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/windows")
Navigates to the Multiple Windows practice page.
Store the Original Window Handle
original_window = driver.current_window_handle
Stores the handle of the original browser window before opening another window.
Open a New Browser Window
driver.find_element(By.LINK_TEXT, "Click Here").click()
Clicks the Click Here link, which opens a new browser window.
Wait Until the New Window Opens
WebDriverWait(driver, 10).until(
lambda browser: len(browser.window_handles) == 2
)
Waits until Selenium detects two browser window handles.
Identify the New Window
new_window = [
handle
for handle in driver.window_handles
if handle != original_window
][0]
Finds the handle of the newly opened browser window by excluding the original window handle.
Switch to the New Window
driver.switch_to.window(new_window)
Moves Selenium’s control to the child browser window.
Close the Child Window
driver.close()
Closes only the currently active browser window.
The original browser window remains open.
Switch Back to the Original Window
driver.switch_to.window(original_window)
Returns Selenium’s control to the original browser window.
Verify Only One Window Exists
assert len(driver.window_handles) == 1
Confirms that only the original browser window remains open.
Verify the Original Page
assert driver.find_element(
By.TAG_NAME,
"h3"
).text == "Opening a new window"
Checks that Selenium has successfully returned to the original webpage.
Close the Browser Session
driver.quit()
Closes the remaining browser window and ends the WebDriver session.
Practical Example
Suppose an e-commerce website opens a product size guide in a separate browser window.
The automation script:
Opens the size guide.
Verifies the information.
Closes the size guide window.
Returns to the product page.
Continues with the purchase process.
Automation Testing Example
Consider an online banking application.
Clicking Terms and Conditions opens a new browser window.
The automation script:
Opens the Terms and Conditions window.
Verifies the page content.
Closes the child window.
Returns to the banking application.
Continues with the transaction.
Real-World Example
Closing browser windows is commonly used in:
Banking applications
Payment gateways
E-commerce websites
HR systems
Government portals
Enterprise applications
Document preview systems
Examples include closing invoices, reports, help pages, product guides, and authentication windows after completing verification.
Advantages of Automating Window Closing
Cleans up unnecessary browser windows.
Supports multi-window workflows.
Prevents resource leaks.
Improves automation stability.
Simulates real user behavior.
Common Mistakes Beginners Make
Using quit() Instead of close()
quit() closes every browser window and ends the session.
Use close() when you only want to close the current browser window.
Forgetting to Switch Back to the Original Window
After closing the child window, always switch back to the original window before continuing.
Closing the Wrong Window
Always verify which window is currently active before calling close().
Assuming the Original Window Becomes Active Automatically
After closing a child window, Selenium does not automatically switch back.
Always call:
driver.switch_to.window(original_window)
Best Practices
Store the original window handle before opening another window.
Close only the window that is no longer needed.
Switch back to the original window after closing the child window.
Verify the remaining window before continuing.
Use
quit()only when the entire test is complete.
Conclusion
Closing browser windows correctly is an important part of Selenium automation. The close() method allows you to close only the active browser window, while quit() ends the entire browser session. Properly switching back to the original window after closing a child window ensures reliable and stable multi-window automation.
Frequently Asked Questions (FAQs)
What is the difference between close() and quit()?
close()closes only the active browser window.quit()closes all browser windows and ends the WebDriver session.
Why should I switch back to the original window after closing a child window?
Because Selenium continues working with the currently selected window. After closing a child window, you must switch back to the original window before interacting with it.
How do I close only the current browser window?
Use:
driver.close()
How do I close all browser windows?
Use:
driver.quit()
Where is window closing commonly used?
Window closing is commonly used after verifying payment pages, reports, help pages, invoices, authentication windows, and external websites.
Key Takeaways
driver.close()closes only the active browser window.driver.quit()closes all browser windows and ends the session.Always store the original window handle before opening another window.
Switch back to the original window after closing a child window.
Verify that the expected window remains open before continuing.
Proper window management improves Selenium automation reliability.
