351

What is the difference between these

  1. Webdriver.Close()
  2. Webdriver.Quit()
  3. Webdriver.Dispose()

Which one to be used and when?

zb226
  • 9,586
  • 6
  • 49
  • 79
Puran Joshi
  • 3,656
  • 2
  • 13
  • 9
  • 2
    The #dispose method appears to have been silently dropped from the WebDriver API. No mention in the changelog, most recent mention I could find was v2.26 api docs, which I can't find the link for anymore. – jordanpg Aug 08 '15 at 19:25
  • I see where the confusion is coming from in the answers below. I think this question was originally a C# question as the methods above (Close, Quit and Dispose) start with a capital letter (C#) not lower case letter (java). Dispose has not been dropped from the C# WebDriver client bindings. – rcasady616 Dec 14 '16 at 05:33
  • Dispose is a .Net pattern and so is not documented in selenium's API docs. In RemoteWebDriver, Quit calls Dispose, which sends a Quit command (DELETE /session/{sessionId}). There are a several places in the client side where the Quit command is intercepted. The FirefoxDriver .net implementation for example will actually do a process.Kill() call if the process doesn't shutdown gracefully. – Tamir Daniely May 14 '18 at 05:01

12 Answers12

301

This is a good question I have seen people use Close() when they shouldn't. I looked in the source code for the Selenium Client & WebDriver C# Bindings and found the following.

  1. webDriver.Close() - Close the browser window that the driver has focus of
  2. webDriver.Quit() - Calls Dispose()
  3. webDriver.Dispose() Closes all browser windows and safely ends the session

The code below will dispose the driver object, ends the session and closes all browsers opened during a test whether the test fails or passes.

public IWebDriver Driver;

[SetUp]
public void SetupTest()
{
    Driver = WebDriverFactory.GetDriver();
}

[TearDown]
public void TearDown()
{
    if (Driver != null)
      Driver.Quit();
}

In summary ensure that Quit() or Dispose() is called before exiting the program, and don't use the Close() method unless you're sure of what you're doing.

Note
I found this question when try to figure out a related problem why my VM's were running out of harddrive space. Turns out an exception was causing Quit() or Dispose() to not be called every run which then caused the appData folder to fill the hard drive. So we were using the Quit() method correctly but the code was unreachable. Summary make sure all code paths will clean up your unmanaged objects by using exception safe patterns or implement IDisposable

Also
In the case of RemoteDriver calling Quit() or Dispose() will also close the session on the Selenium Server. If the session isn't closed the log files for that session remain in memory.

rcasady616
  • 3,155
  • 1
  • 14
  • 17
  • How did you update your exceptions? Just go to each one and add a quit()? – Mark Mayo Jul 23 '13 at 02:12
  • 1
    In a test frame work like NUnit, JUnit you only have to make sure that the Dispose() is in the TestTeardown() or TestFixtureTeardown(). In C# you can just use a "using" statement and Dispose is always called if an exception is raised. You can do something similar in Java by using a try catch finally and put the Dispose in the finally. – rcasady616 Jul 27 '13 at 01:33
  • @rcasady616 regarding IDisposable and a NUnit test or similar, do you have any sample that you can provide? We have a [Destructor](http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx) at the test level that calls Dispose() on webdriver, but we are not sure if this will be enough or if we would have to Implement IDisposable on a NUnit test class. Have you looked into how to have a Test class implement IDisposable and how that would be called from the test framework at run time? Would love to know! – John Babb Aug 05 '14 at 10:06
  • John I always call Quit() in the Teardown() method, this way pass or fail the driver and session are disposed properly. I hope that answers your question. Here is a link to a sample on github https://gist.github.com/rcasady616/b2488e72d062c95dc97b#file-webdriverdisposepattern – rcasady616 Aug 15 '14 at 04:01
  • The difference between `Quit()` and `Dispose()` is not clear to me. – Mr. Bultitude May 19 '16 at 22:04
  • 1
    @rcasady616 This is the best explanation I have got. Thanks for it and I think it's worthy for upvote. – RNS May 04 '17 at 12:25
  • do you need to actually `quit` the session to start a new instance with a new user agent string or can i simply `close` the browser and create a new instance and manually apply a new user agent string as im doing right now?? – oldboy Sep 16 '18 at 06:02
  • @Anthony It really depends on your needs, you can close and open new windows in the same session if that helps your situation. But just remember to call Quit or Dispose to terminate the session when your done or the result will be that files are abandoned in your appData folder. I have known multiple people who have almost filled there hard drives because they used Close explicitly. – rcasady616 Oct 04 '18 at 00:29
  • do you ***need*** to `quit` a session to ***effectively*** use a new user agent string or, in other words, make the website believe it's a new user agent tho?? – oldboy Oct 06 '18 at 18:22
  • 2
    @Mr.Bultitude well, that's understandable, considering there *is* no difference ^^ According to this answer, `Quit()` is a redirect to `Dispose()` – PixelMaster Jul 04 '19 at 16:53
  • 1
    @PixelMaster, Yes are 100% correct, its basically a redirect. Not being the one who wrote the code in Selenium I can only guess as to why both methods are present in the C# client bindings. Probably they want to keep with a common method for "Quitting" across client languages, but also wanted to use the driver in a "using" statement syntax (witch would require the Dispose() method). That's my best guess, either that or the code needs to be cleaned up. – rcasady616 Jul 07 '19 at 20:01
55

Close() - It is used to close the browser or page currently which is having the focus.

Quit() - It is used to shut down the web driver instance or destroy the web driver instance(Close all the windows).

Dispose() - I am not aware of this method.

Manigandan
  • 5,004
  • 1
  • 28
  • 47
  • 4
    If you're using the .NET language bindings, the `Quit` and `Dispose` methods should be synonyms for one another. In other words, `Quit` calls `Dispose`. – JimEvans Feb 25 '13 at 16:57
45

driver.close and driver.quit are two different methods for closing the browser session in Selenium WebDriver. Understanding both of them and knowing when to use each method is important in your test execution. Therefore, I have tried to shed some light on both of these methods.

driver.close - This method closes the browser window on which the focus is set. Despite the familiar name for this method, WebDriver does not implement the AutoCloseable interface.

driver.quit – This method basically calls driver.dispose a now internal method which in turn closes all of the browser windows and ends the WebDriver session gracefully.

driver.dispose - As mentioned previously, is an internal method of WebDriver which has been silently dropped according to another answer - Verification needed. This method really doesn't have a use-case in a normal test workflow as either of the previous methods should work for most use cases.

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

The above explanation should explain the difference between driver.close and driver.quit methods in WebDriver. I hope you find it useful.

The following website has some good tips on selenium testing : Link

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Anudeep Samaiya
  • 1,910
  • 2
  • 28
  • 33
  • I also noticed that the IEDrivers that are used for working with IE based automation does not exit if you use driver.close() method. I had to use driver.quit() to terminate it. It is because the quit() method also appears to clean up resources unlike close(), although, in my example, I have a single tab always. – Pavan Dittakavi Jun 22 '20 at 06:18
16

quit(): Quits this driver, closing every associated window that was open.

close() : Close the current window, quitting the browser if it's the last window currently open.

okoboko
  • 4,332
  • 8
  • 40
  • 67
Ran Adler
  • 3,587
  • 30
  • 27
  • 2
    this answer misses the key difference: quit() will stop the underlying webdriver service, while close() does not. pro-tip: don't ever call close() if only one window remains – Corey Goldberg Sep 09 '19 at 00:41
10

close() is a webdriver command which closes the browser window which is currently in focus. Despite the familiar name for this method, WebDriver does not implement the AutoCloseable interface.

During the automation process, if there are 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:

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 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.

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

For Above Ref : click here

Dispose Command Dispose() should call Quit(), and it appears it does. However, it also has the same problem in that any subsequent actions are blocked until PhantomJS is manually closed.

Ref Link

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Dhaval Atri
  • 201
  • 2
  • 6
  • 15
6

Based on an issue on Github of PhantomJS, the quit() does not terminate PhantomJS process. You should use:

import signal
driver = webdriver.PhantomJS(service_args=service_args)
# Do your work here

driver.service.process.send_signal(signal.SIGTERM)
driver.quit()

link

Wesam Na
  • 2,364
  • 26
  • 23
5

close():- Suppose you have opened multiple browser windows with same driver instance, now calling close() on the driver instance will close the current window the driver instance is pointed to. But the driver instance still remain in memory and can be used to handle other open browser windows.

quit():- If you call quit() on the driver instance and there are one or more browser windows open, it will close all the open browser windows and the driver instance is garbage collected i.e. removed from the memory. So now you cannot use this driver instance to do other operation after calling quit() on it. If you do it will throw an Exception.

dispose():- I don't think there is a dispose method for a WebDriver instance.

You can go to the this selenium official java doc link for reference.

Prasanta Biswas
  • 761
  • 2
  • 14
  • 26
3

Selenium WebDriver

  1. WebDriver.Close() This method is used to close the current open window. It closes the current open window on which driver has focus on.

  2. WebDriver.Quit() This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. WebDriver.Quit() calls Dispose.

  3. WebDriver.Dispose() This method closes all Browser windows and safely ends the session

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
s10v10s
  • 63
  • 13
3

Difference between driver.close() & driver.quit()

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.

3

There are two difference between Quit and close.

  1. As Quit() closes all the windows opened by program whereas Close() will close the correct window where focus on set.
  2. Quit () - Consider, WebDriver driver = new ChromeDriver(); Above statement will create a session ID. Same session ID maintained for entire session. Session ID - 73e6d7c2ae55d9f059ad1cce248adb75

after entering the url the same Session ID - 73e6d7c2ae55d9f059ad1cce248adb75

After the currentUrl() the session ID - 73e6d7c2ae55d9f059ad1cce248adb75

After quit() the browser the session ID - null

if user try to execute any statement, session ID null will send to server. On seeing this, server will send the exception - Session ID is null. Using WebDriver after calling quit()?

In Close(), Random Session ID(only one) will be created and maintained for entire session. After executing the Close(), session Will be invalid or expired. Exception

So, in quit() the session is null and in close() session id is invalid.

1

My understanding is driver.close(); will close the current browser, and driver.quit(); will terminate all the browser that.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
BIUbiubiu
  • 29
  • 2
-2

Difference between driver.close() & driver.quit() with respect to session ID.

driver.close – It closes the the browser window on which the focus is set and if driver instance is used after driver.close then selenium throw NoSuchSessionException: invalid session id

driver.quit – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully and if driver instance is used after driver.quit then selenium throw NoSuchSessionException: session id is null

Vaibhav Jagdale
  • 229
  • 2
  • 11