问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/15787777/how-to-import-and-export-firefox-profile-for-selenium-webdriver-in-python