0

After closing the new created firefox window (FirefoxDriver) a folder named "anonymousXXXXXXX.webdriver-profile" remains in the temp directory (AppData\Local\Temp). When using the Dispose method or the using statement the folder for the new created firefox profile in the temp directory is removed but also the temporary firefox instance closes instantly.

IWebDriver browser = new FirefoxDriver();

browser.Url = "https://www.google.com/";
browser.Navigate();
browser.Dispose();

How do I call the dispose method after exiting the firefox instance?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user2943268
  • 85
  • 10
  • **When** do you want to close FireFox instance? I suppose that's the place to dispose FireFoxDriver... – Adriano Repetti Oct 31 '13 at 23:13
  • I'd like to do this when the firefox window gets closed by the user. – user2943268 Oct 31 '13 at 23:16
  • Did you try to check WindowHandles property? – Adriano Repetti Oct 31 '13 at 23:24
  • Should I use the browser.WindowHandles.Count property and check this value using a timer? – user2943268 Nov 01 '13 at 08:43
  • Well it looks (a lot) like a _bad small_ solution but I can't imagine anything else. – Adriano Repetti Nov 01 '13 at 08:45
  • Ok, but my program exits out of the Main() method after starting the timer. How can I fix this? – user2943268 Nov 01 '13 at 08:47
  • Keep your program _busy_ in a DoEvents() loop. while (IsFfStillOpen()) { Application.DoEvents(); } – Adriano Repetti Nov 01 '13 at 08:54
  • After starting the timer: `while (browser.WindowHandles.Count > 0) System.Windows.Forms.Application.DoEvents();` The timer_tick Event looks like that: `if (browser.WindowHandles.Count == 0) browser.Dispose();` The folder in the temp directory still remains. – user2943268 Nov 01 '13 at 09:04
  • What is the issue here? What are you trying to do? What doesn't it do? What version of Selenium are you using? Why does it matter about the temporary directory? – Arran Nov 01 '13 at 09:22
  • Yes, you are right. Maybe it would be better to let the temporary directory remain. – user2943268 Nov 01 '13 at 09:25
  • @user2943268, you can clear it up yourself if you really want to. You can also stop Selenium creating the temporary directory in the first place by creating a user profile for the FirefoxDriver to use, it then won't create anything: http://stackoverflow.com/questions/6787095/how-to-stop-selenium-from-creating-temporary-firefox-profiles-using-web-driver – Arran Nov 01 '13 at 10:24
  • @Arran Even providing a user profile, a temp directory copy will still be made, since multiple instances have to be accounted for. – JimEvans Nov 04 '13 at 14:52

1 Answers1

3

The Firefox driver creates a temporary profile in the temp directory. Full stop. There is no way to suppress this behavior. Even if you provide a profile to use, the driver will copy the provided profile into the temp directory, using it as a template. The reason for this behavior is to prevent conflicts when multiple instances of FirefoxDriver are driving different instances of Firefox.

It sounds like you're wanting to use WebDriver to create an instance of Firefox, that you then allow a user to use, turning complete control of the Firefox instance to the user. In that case, you've surrendered the ability to monitor the lifetime of the Firefox process to the user, and WebDriver can no longer do so. You'll need to either maintain your instance of the FirefoxDriver class and give the user an alternative way to close Firefox, forcing them to do so through your code, in which you can call driver.Quit(); or you'll need to figure out a method to clean the temp directory for you. The former is impractical; the latter is nearly impossible to get right.

JimEvans
  • 27,201
  • 7
  • 83
  • 108