Open In App

Difference between driver.dispose(), driver.close() and driver.quit()

Last Updated : 12 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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()

Parameters

driver.close()

driver.quit()

driver.dispose()

Execution Scope

Window/Tab-specific

Session-specific

Instance-specific

Effect on WebDriver Session

Doesn’t terminate

Terminates along with windows

Doesn’t terminate

Resource Release

Closes a specific window/tab in focus

Terminates WebDriver and associated windows/tabs

Releases resources for a specific WebDriver instance

Effect on Active Window/Tab

Closes the active window/tab

Closes the active window/tab

No effect

Impact on Multiple Windows/Tabs

Doesn’t affect

Closes all windows/tabs

Doesn’t affect

Handling Single Window/Tab

Closes the single window/tab

Closes the single window/tab

Doesn’t close

Effect on WebDriver Server

No impact

Sends termination signal to WebDriver server

No impact

Graceful Termination

Closes window/tab without ending session

Ends WebDriver session

Doesn’t end the session

Memory Usage

Resources used by the window/tab

All WebDriver resources

Resources used by instance

Commonly Used in

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads