Close vs Quit in Selenium: Key Differences and Usage
close() closes the current browser window or tab, while quit() closes all browser windows and ends the WebDriver session. Use close() to shut one window and quit() to end the entire browser session.Quick Comparison
Here is a quick side-by-side comparison of close() and quit() methods in Selenium:
| Factor | close() | quit() |
|---|---|---|
| Action | Closes the current browser window or tab | Closes all browser windows and ends WebDriver session |
| Effect on WebDriver session | Session remains active if other windows are open | Session is terminated completely |
| Use case | When you want to close one window but keep others open | When you want to end the entire browser session |
| Impact on tests | Can continue testing other windows | Ends all testing as browser closes |
| Common scenario | Closing a popup window | Ending test and cleaning up resources |
Key Differences
The close() method in Selenium is designed to close only the currently focused browser window or tab. If your test has multiple windows or tabs open, calling close() will shut just one of them, leaving the others and the WebDriver session active. This allows you to continue interacting with other windows in your test.
On the other hand, quit() is more final. It closes all open browser windows and completely ends the WebDriver session. This means the browser process is terminated, and you cannot interact with any windows afterward without starting a new session. It is typically used at the end of a test to clean up resources.
In summary, close() is for closing a single window while keeping the session alive, and quit() is for shutting down everything related to the WebDriver session.
Code Comparison
Example showing how to use close() to close the current window:
from selenium import webdriver # Start WebDriver session driver = webdriver.Chrome() driver.get('https://example.com') # Open a new tab driver.execute_script("window.open('https://google.com', '_blank');") # Switch to the new tab windows = driver.window_handles driver.switch_to.window(windows[1]) # Close the current tab (Google) driver.close() # Switch back to the first tab driver.switch_to.window(windows[0]) # Continue testing on the first tab print(driver.title) # Quit at the end driver.quit()
quit() Equivalent
Example showing how to use quit() to close all browser windows and end the session:
from selenium import webdriver # Start WebDriver session driver = webdriver.Chrome() driver.get('https://example.com') # Open a new tab driver.execute_script("window.open('https://google.com', '_blank');") # Quit closes all windows and ends session driver.quit()
When to Use Which
Choose close() when your test involves multiple browser windows or tabs and you want to close only the current one without ending the entire session. This is useful for handling popups or switching between windows.
Choose quit() when your test is complete and you want to close all browser windows and clean up the WebDriver session. This ensures no browser processes remain running after your tests finish.
Key Takeaways
close() closes only the current browser window or tab.quit() closes all browser windows and ends the WebDriver session.close() to manage multiple windows without ending the session.quit() to clean up and end tests completely.quit() at the end of tests to free resources.