How to import and export FireFox profile for selenium webdriver in python

半腔热情 提交于 2019-12-24 10:47:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!