1

How can I import and export a webdriver FireFox profile?

What I wold like to do is something like:

from selenium import webdriver

#here I want to import the FF profile from a path

if profile:
  driver = webdriver.Firefox(profile)
else:
  #this is the way I get the WebDriver currently
  driver = webdriver.Firefox()

#doing stuff with driver

#Here I want to save the driver's profile 
#so I could import it the next time
dnlcrl
  • 5,022
  • 3
  • 32
  • 40
  • You need to be more specific. You can load a saved profile, or create one on the fly via the Firefox profile object, but what "profile exists" means is too vague to provide any meaningful answer without more context. How did you create it? Where and how is it stored? – Silas Ray Apr 03 '13 at 13:02
  • I improved the question, thanks for the suggestion – dnlcrl Apr 03 '13 at 13:13

1 Answers1

4

You have to decide on a location to store the cached profile, then use functions in the os library to check if there is a file in that location, and load it. To cache the profile in the first place, you should be able to get the path to the profile from webdriver.firefox_profile.path, then copy the contents to your cache location.

All that said, I'd really recommend against this. By caching the profile created at test runtime, you are making your test mutate based upon previous behavior, which means it is no longer isolated and reliably repeatable. I'd recommend that you create a profile separately from the test, then use that as the base profile all the time. This makes your tests predictably repeatable. Selenium is even set up to work well with this pattern, as it doesn't actually use the profile you provide it, but instead duplicates it and uses the duplicate to launch the browser.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63