Hannu
Hannu

Reputation: 12205

Selenium - Retaining firefox cache and history files

Is there a way to disable Selenium creating a temporary directory and profile when it starts Firefox?

I fully understand why Selenium does things as it does. I am just experimenting it as I try to create Firefox caches and histories with it for computer forensic training purposes. To this end, I have set up a clean virtual machine with a pristine user account. I can now run a Python script with selenium API to start firefox, visit a couple of web pages and shut down.

THe problem is, it leaves nothing behind. This is of course excellent if you are using selenium in its original purpose, but it thwarts my work by deleting everything.

So is there a way to disable the temporary profile creation and just start Firefox as it would start if ran by the user without Selenium.

Addition 5:34PM: Java API documentation mentions a system property webdriver.reap_profile that would prevent deletion of temporary files. I went to the source of the problem and it appears this does not appear in Python WebDriver class:

def quit(self):
        """Quits the driver and close every associated window."""
        try:
            RemoteWebDriver.quit(self)
        except (http_client.BadStatusLine, socket.error):
            # Happens if Firefox shutsdown before we've read the response from
            # the socket.
            pass
        self.binary.kill()
        try:
            shutil.rmtree(self.profile.path)
            if self.profile.tempfolder is not None:
                shutil.rmtree(self.profile.tempfolder)
        except Exception as e:
            print(str(e))

Deletion of files upon quit appears to be unconditional. I will solve this in my case by injecting

return self.profile.path

just after self.binary.kill(). This probably breaks all sorts of things and is a horrible thing to do but it appears to do exactly what I want it to do. The return value tells the calling function the random name of the temporary directory under /tmp. Not elegant but appears to work.

Upvotes: 2

Views: 1568

Answers (1)

Hannu
Hannu

Reputation: 12205

Addition 5:34PM: Java API documentation mentions a system property webdriver.reap_profile that would prevent deletion of temporary files. I went to the source of the problem and it appears this does not appear in Python WebDriver class:

def quit(self):
        """Quits the driver and close every associated window."""
        try:
            RemoteWebDriver.quit(self)
        except (http_client.BadStatusLine, socket.error):
            # Happens if Firefox shutsdown before we've read the response from
            # the socket.
            pass
        self.binary.kill()
        try:
            shutil.rmtree(self.profile.path)
            if self.profile.tempfolder is not None:
                shutil.rmtree(self.profile.tempfolder)
        except Exception as e:
            print(str(e))

Deletion of files upon quit appears to be unconditional. I will solve this in my case by injecting

return self.profile.path

in /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py just after self.binary.kill(). This probably breaks all sorts of things and is a horrible thing to do but it appears to do exactly what I want it to do. The return value tells the calling function the random name of the temporary directory under /tmp. Not elegant but appears to wor after a recompile.

If a more elegant solution exists, I would be happy to flag that as the correct one.

Upvotes: 1

Related Questions