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

When we work with Selenium Webdriver multiple browsers with multiple tabs open up. In order to close these tabs, we have two different web driver commands, close() and quit(). We will look at the working of each command in detail.

close() in selenium

close() is a web driver command that is used to close the currently focused browser window. The rest of the browsers that are opened will not be closed. Also, the driver session remains active and does not shut down.

from selenium import webdriver 
  
# Here Chrome  will be used 
driver = webdriver.Chrome() 
  
# URL of website 
url = "https://www.codespeedy.com/"
  
# Opening the website 
driver.get(url) 
  
# Closes the current window 
driver.close()

The above code will first load the webpage and then will close it due to the use of close() command.

Also learn: How to close the browser window at the end of a Selenium test program

quit() in selenium

quit() is a web driver command which is used to close all the browser windows by calling the driver.dispose method. It is also used to terminate the web driver session. This quit() command is used at the end of the program to close the web driver session properly and clear off the memory from the files preventing memory leak errors.

from selenium import webdriver 
  
# Here Chrome  will be used 
driver = webdriver.Chrome() 
  
# URL of website 
url = "https://www.codespeedy.com/"
  
# Opening the website 
driver.get(url) 
  
# All windows related to driver instance will quit 
driver.quit()

The above code will first load the webpage and then will quit the window due to the use of quit() command.

NOTE: If only a single browser window is opened then both the close() and quit() commands will work in the same manner.

Leave a Reply

Your email address will not be published.