arsenik
arsenik

Reputation: 1017

Using Firefox with Selenium webdriver does not record history

I am running FF 39 with python Selenium webdriver 2.47.

from selenium import webdriver
profile = webdriver.FirefoxProfile('C:\\Users\\Mike\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kgbrptal.default')
b = webdriver.Firefox(profile)

In C:\Users\Mike\AppData\Roaming\Mozilla\Firefox\Profiles\kgbrptal.default\places.sqlite I should see history updated. But nothing get updated?

I am expecting all actions been recorded in the profile database.

Upvotes: 1

Views: 1339

Answers (1)

Louis
Louis

Reputation: 151441

The Problem

The problem you're facing is that Selenium creates a new and temporary profile from the profile you tell it to use. It does this because it has to add an add-on that it uses to establish communication between the browser and your script. If it did not copy your profile to a new location, the add-on would be added to the profile you gave to Selenium. Many users would find this unacceptable. So it creates a new temporary profile from the one you specify and deletes it after it is done.

A Solution

There is no flag you can give it to prevent the deletion of the temporary profile. However, you could save it before calling the .quit() method. The code that deletes the temporary profile goes:

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

If you assigned your WebDriver to b, you could do:

shutil.copytree(b.profile.path, "where/you/want/to/save/it", 
                ignore=shutil.ignore_patterns("parent.lock", 
                                              "lock", ".parentlock"))
b.quit()

The ignore is needed to avoid copying some lock files that would cause the copy to fail.

Here's a complete example:

import shutil
from selenium import webdriver

profile = webdriver.FirefoxProfile(path_to_your_profile)
driver = webdriver.Firefox(profile)
driver.get("http://google.com")
shutil.copytree(driver.profile.path, "./here",
                ignore=shutil.ignore_patterns("parent.lock", 
                                              "lock", ".parentlock"))
driver.quit()

You need to set path_to_your_profile to the actual path. It will copy the temporary profile to a subdirectory named here in the current working directory of the script.

Upvotes: 1

Related Questions