24

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/')
    sel.start()
    # do stuff

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

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 Oct 11 '11 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 Oct 12 '11 at 13:15
  • 1
    Okay. I will try .quit(). I found that .stop() will stop the server, but not close the window. – Aaron Shaver Oct 12 '11 at 17:30
  • .quit() did not work – Aaron Shaver Oct 12 '11 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 Oct 12 '11 at 23:52

10 Answers 10

20

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/')
        self.selenium.start()

    def tearDown(self):
        self.selenium.stop()

    def test_bar(self):
        self.selenium.open("/somepage")
        #and so forth
19

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. – Vince Bowdren Aug 18 '16 at 17:00
  • Hii Vince Bowdren ,Thank You for your wonderful suggestion. – Sneha Singh Aug 22 '16 at 12:09
9

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

In Java :

WebDriver driver;      
driver.quit();

In C# :

IWebDriver Driver;
Driver.Quit();
3

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

self.driver.stop_client()
self.driver.close()
  • May we just call driver.quit() for that? – Nam G VU Jul 20 '17 at 2:47
2
WebDriver driver;      
driver.quit();

Above will close all open browser windwos.

And

WebDriver driver;      
driver.close();

This will close current browser window in focus.

1

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.quit();
    driver = null;
}
  • why the last command? driver=null; – Yash Apr 30 '14 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. – user1829657 May 5 '14 at 11:56
0

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 Jun 4 '14 at 10:58
0

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 Sep 29 '16 at 15:12
0

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

@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 {
        chromeDriver.close()
        chromeDriver.quit()
    }

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.

0

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

time.sleep(2) driver = webdriver.Chrome()

driver.implicitly_wait(10)

Maximizes the window

driver.maximize_window()

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

def close(): driver.close() close()

And this works for me in Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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