How to use close() and quit() method in Selenium Python ?

While doing stuff with selenium multiple browsers with multiple tabs will normally opens in order to close these tabs close() and quit() methods are used. close() method is used to close the current browser window on which the focus is set, on the other hand quit() method essentially calls the driver.dispose method that successively closes all the browser windows and ends the WebDriver session graciously.

Syntax :

driver.close()
driver.quit()

Argument :

Both methods takes no argument

Action performed :
close() method closes the current window.
quit() method quits the driver instance, closing every associated window, which is opened.



Code for close method() :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing webdriver from selenium
from selenium import webdriver
  
# Here Chrome  will be used
driver = webdriver.Chrome()
  
# URL of website
  
# Opening the website
driver.get(url)
  
# Closes the current window
driver.close()

chevron_right


Output : The webpage will loaded then closes due to close() method.

Code for quit() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing webdriver from selenium
from selenium import webdriver
  
# Here Chrome  will be used
driver = webdriver.Chrome()
  
# URL of website
  
# Opening the website
driver.get(url)
  
# All windows related to driver instance will quit
driver.quit()

chevron_right


Output : The webpage will be loaded then window get quit due to quit() method.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.