I have googled for the answer, but the .stop() so frequently mentioned doesn't work for me. The Chrome window the test was running in remains open.

def test_getResults(self):
    sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
    # do stuff

def tearDown(self):
    sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')

Any ideas? I'm using Selenium Server 2.8.0 with Python 2.6 and mostly using Chrome 14 windows to test.

  • Is there a quit method?
    – user246
    Commented Oct 11, 2011 at 22:21
  • Yes there is - browser.quit(); Although when I used to run these types of tests before switching to WebDriver I used to have in my TearDown - self.selenium.stop() That usually did it for me.
    – MichaelF
    Commented Oct 12, 2011 at 13:15
  • 1
    Okay. I will try .quit(). I found that .stop() will stop the server, but not close the window.
    – Aaron
    Commented Oct 12, 2011 at 17:30
  • .quit() did not work
    – Aaron
    Commented Oct 12, 2011 at 17:37
  • 1
    I verified in C# that webdriver.Quit() closes a firefox window, I didn't try it with a chrome driver.
    – Sam Woods
    Commented Oct 12, 2011 at 23:52

12 Answers 12


driver.close() and driver.quit() are two different methods for closing the browser session in Selenium WebDriver.

driver.close() - It closes the the browser window on which the focus is set.

driver.quit() – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully.

You should use driver.quit() whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit() at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.

  • 1
    Hi Sneha Singh, and welcome to Stack Overflow. Your answer is a good start, but you could make it better by adding a bit more context; can you describe the difference between the driver.close and driver.quit methods and how you would choose between them? Maybe including links to official documentation would help too - see the advice at How to Answer. Commented Aug 18, 2016 at 17:00
  • Does the context manager with webdriver.Firefox() as wd take care of quit for you?
    – Noumenon
    Commented Oct 27, 2019 at 15:34
  • This is a very old question, but maybe my expirience will help someone. I wanted to close a browser page but do not close the browser itself. driver.close() did not worked as expected, so I just did driver.get('about:blank') instead. Commented Nov 4, 2020 at 16:57

You're actually creating a second Selenium session in your tearDown() function. You need to put the session created in setUp() into an instance variable, then close that session in tearDown().

class TestFoo(unittest.TestCase):
    def setUp(self):
        self.selenium = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')

    def tearDown(self):

    def test_bar(self):
        #and so forth

I have worked with Web Driver in both java and C# and I use

In Java :

WebDriver driver;      

In C# :

IWebDriver Driver;

In Python, using selenium webdriver for Chrome, I needed to call stop_client() before close():

from selenium import webdriver

in setUp():

options = webdriver.chrome.options.Options()
options.add_argument("--disable-extensions") # optional and off-topic, but it conveniently prevents the popup 'Disable developer mode extensions' 
self.driver = webdriver.Chrome(chrome_options=options)

In tearDown():

  • 2
    May we just call driver.quit() for that?
    – Nam G VU
    Commented Jul 20, 2017 at 2:47
WebDriver driver;      

Above will close all open browser windwos.


WebDriver driver;      

This will close current browser window in focus.


Using TestNG and Java.
Assume this method is located in some BaseTest class which is inherited by test class, so try this:

@AfterClass(alwaysRun = true)
protected void tearDown() {
    driver = null;
  • why the last command? driver=null;
    – Yash
    Commented Apr 30, 2014 at 6:39
  • I use it in scope init driver logic, like if null then new driver instance is being creating. That command is optional however. Commented May 5, 2014 at 11:56

You can use either driver.close(); or driver.quit();.

Use close() for one browser to close and quit() is to close all browsers using webdriver. But why close() is not closed at runtime, but it is closed at debugging I don't know.

  • 2
    Your answer is weak (because you don't give much other information) and doesn't add much to the question and existing answers. You also asked a different question which really should be posted as a separate question (with a lot more information)
    – Kate Paulk
    Commented Jun 4, 2014 at 10:58

This is what worked for me:

    taskkill /f /fi "pid gt 0" /im iexplore.exe

I have this in my pre-build events in the Visual Studio Solution. This is can be run from the command line, just call it in your project and you should be all set.

I also have the following line to close the the IE driver in case of failure to execute driver.Quit() or .Close()

  taskkill /f /fi "pid gt 0" /im IEDriverServer.exe
  • 2
    Could you indicate how the questioner could run this within their code and with chrome? I assume they want to incorporate it into their suite and not perform a one off action
    – ECiurleo
    Commented Sep 29, 2016 at 15:12

If you use headless mode you still see them not closing try this solution 1) Get the driver as singleton

class BrowserInstance {

ChromeDriver getDriver(){
    ChromeOptions options = new ChromeOptions()
    options.addArguments("--headless --disable-gpu")
    return new ChromeDriver(options)

2) Use Close and quit in finally block

 finally {

Result: you will be using only one instance at time and if you see task manager you will not find chromedriver and chrome process hanging.


Maybe my solution will be not a super smart one, but before each automated test case I put:

driver = webdriver.Chrome()


Maximizes the window


And to close all windows of the test case I use:

def close():

And this works for me in Python.


This will close only window, your selenium session still exists and you can execute your further processing using selenium driver.

  • Will this close one window and move to another? Or will it just switch between 2 windows keep both open? Commented Dec 2, 2021 at 10:46

At least a part of the problem is likely because of the Python local vs global behaviour of variables. Adding some comments to your code:

def test_getResults(self):
    """ Get a test result """
    # The next line creates a local selenium web driver instance and connects it
    sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/') 
    # do stuff
    # At this point sel goes out of scope but we have done nothing to stop it

def tearDown(self):
    """End of testing!"""
    # The next line creates a local selenium web driver instance and connects it
    sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
    sel.close() # Closes the local sel instance
    sel.stop() # Stops the local sel instance

So the sel instance that you create in test_getResults is not, by default, the one that you stop. Unless the selenium function that you do not show is:

  1. Checking for an existing instance of the driver and if it exists calling get with the parameters then returning the handle for the driver
  2. If the driver does not exist create and do get then return the handle

Since you give a self parameter to each method I presume that the methods are being created as class members for the same class instance. In that case something like:

def test_getResults(self):
    """ Get a test result """
    # The next lines gets or creates a selenium web driver instance and connects it
    if not hasattirbute(self, 'sel) or self.sel is None:
        # No running instance available
        self.sel = selenium('localhost', 4444, "*chrome", 'http://blackpearl/')
        sel = self.sel # Local variable to point to the class member
        sel = self.sel
    # do stuff
    # At this point sel goes out of scope but but self.sel does not

def tearDown(self):
    """End of testing!"""
    if hasattirbute(self, 'sel) and self.sel is not None:
        del self.sel # just in case self persists
        print("WARNING: tearDown called with nothing to do!")

Personally I would also import atexit and when creating self.sel register self.tearDown as an exit handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.