0
0
Selenium-pythonComparisonBeginner · 3 min read

Close vs Quit in Selenium: Key Differences and Usage

In Selenium, 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:

Factorclose()quit()
ActionCloses the current browser window or tabCloses all browser windows and ends WebDriver session
Effect on WebDriver sessionSession remains active if other windows are openSession is terminated completely
Use caseWhen you want to close one window but keep others openWhen you want to end the entire browser session
Impact on testsCan continue testing other windowsEnds all testing as browser closes
Common scenarioClosing a popup windowEnding 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:

python
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()
Output
Example Domain
↔️

quit() Equivalent

Example showing how to use quit() to close all browser windows and end the session:

python
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.
Use close() to manage multiple windows without ending the session.
Use quit() to clean up and end tests completely.
Always call quit() at the end of tests to free resources.