Difference between driver.dispose(), driver.close() and driver.quit()
Last Updated :
12 Dec, 2023
In Selenium driver.close(), driver.quit(), driver.dispose() methods are used to close the browser windows in their own functionality. Each method serves a similar purpose but has different functionality. The driver.close() method is used to close the current browser window. The driver.quit() method is used to close all the browser windows and finally. The driver.dispose() is used for specific WebDriver implementations like .NET.
driver.close() Method
The driver.close() method is used to close the current browser window which is in focus for WebDriver.
- Even if there are multiple windows or tabs opened in the browser, driver.close() method will not close all the windows or tabs present in the browser.
- It will close only the currently focused window and not end the WebDriver session.
- So, diver.close() method is applicable only for currently focused windows or tabs.
Syntax:
driver.close()
driver.quit() Method
driver.quit() method is used to close all browser windows which are running and it will end the webdriver session.
- driver.quit() is suitable when the tester wants to end the entire script/test to ensure all browser windows are closed.
- After calling driver.quit() method further actions on webdriver will be through exceptions as webdriver session is ended.
Syntax:
driver.quit()
driver.dispose() Method
driver.dispose() method is used for specific webdriver implementations like .NET.
- It is used in languages that require manual resource management like .NET to release memory and resources associated with the WebDriver and is not used commonly in Python or other languages.
Syntax:
driver.Dispose()
Differences between driver.close(), driver.quit() and driver.dispose()
Window/Tab-specific
|
Session-specific
|
Instance-specific
|
Doesn’t terminate
|
Terminates along with windows
|
Doesn’t terminate
|
Closes a specific window/tab in focus
|
Terminates WebDriver and associated windows/tabs
|
Releases resources for a specific WebDriver instance
|
Closes the active window/tab
|
Closes the active window/tab
|
No effect
|
Doesn’t affect
|
Closes all windows/tabs
|
Doesn’t affect
|
Closes the single window/tab
|
Closes the single window/tab
|
Doesn’t close
|
No impact
|
Sends termination signal to WebDriver server
|
No impact
|
Closes window/tab without ending session
|
Ends WebDriver session
|
Doesn’t end the session
|
Resources used by the window/tab
|
All WebDriver resources
|
Resources used by instance
|
Closing a specific window/tab
|
Ending a complete test or session
|
Resource management scenarios
|
Conclusion
All the three methods will be used to close the browsers based on requirement. The driver.quit() and driver.dispose() will close the all browser tabs whereas driver.close() method will close the currently focused window.
Share your thoughts in the comments
Please Login to comment...