What is close() and quit() commands in Selenium Webdriver?

| | 2 min read

In Selenium Webdriver, a browser session can be closed using two webdriver commands: close() and quit(). The situations in which they are used are briefly explained below:

close()

close() is a webdriver command that closes the browser window currently in focus.

During the automation process, if there is more than one browser window opened, then the close() command will close only the current browser window, which is having focus at that time. The remaining browser windows will not be closed. The following code can be used to close the current browser window:

driver.close() //where, ‘driver’ is the Webdriver object.

quit()

quit() is a webdriver command which calls the driver.dispose method, which in turn closes all the browser windows and terminates the WebDriver session. If we do not use quit() at the end of the program, the WebDriver session will not be closed properly, and the files will not be cleared off memory. This may result in memory leak errors.

The following code can be used to close all the browser windows:

driver.quit()   //where, ‘driver’ is the Webdriver object.

If the Automation process opens only a single browser window, the close() and quit() commands work similarly. Both will differ in their functionality when there is more than one browser window opened during Automation.

I hope you find these tips useful. Please feel free to get in touch with us for any queries.

If you are interested in automated testing, you may also like reading Automating Web Testing with Playwright: An Example Testing a Job Listing Application This in-depth article talks about how to do an end to end automated testing with Playwright, a tool similar to Selenium.