Python / Selenium / Firefox: Can't start firefox with specified profile path
I have spent around 2 hours (yes im so slow) guessing why didn't work. I've found why profile doesn't save back.
Certainly, the profile passed to FirefoxProfile("myprofile/full/path")
is used on the run, but, it's not saved back because (and maybe not obvious) selenium is for testing and testings should run with no cache, no profiles at all, clean as possible.
Profile capability was (maybe) build to allow install certain extensions and settings before run test, but not for save them.
The trick was to print out print driver.firefox_profile.path
. It does differ from usual ones, with name tmp/tmpOEs2RR/webdriver-py-profilecopy instead instead of just tmp/tmpOEs2RR/, so reading that you should guess that a profile is being used.
Now, the only remaining thing is to get it back :)
Run this script, install something, edit something then run it again ;) :
#!/usr/bin/env python
#! -*- coding: utf-8 -*-
import selenium
from selenium import webdriver
import os, sys, time
# 1- set profile
profile = os.path.dirname(sys.argv[0]) + "/selenita"
fp = webdriver.FirefoxProfile(profile)
driver = webdriver.Firefox(firefox_profile=fp)
# 2- get tmp file location
profiletmp = driver.firefox_profile.path
# but... the current profile is a copy of the original profile :/
print "running profile " + profiletmp
driver.get("http://httpbin.org")
time.sleep(2)
raw_input("Press a key when finish doing things") # I've installed an extension
# 3- then save back
print "saving profile " + profiletmp + " to " + profile
if os.system("cp -R " + profiletmp + "/* " + profile ):
print "files should be copied :/"
driver.quit()
sys.exit(0)
U can follow that schema or simple edit the FirefoxProfilea accordingly to your needs.
Thanks to @m3nda i found a simmilar and terrible solution for windows und python 3. As Jake Hilborn noticed it's not working anymore. The problem is, that driver.firefox_profile.path seems to be the tmp profile direktory, but this is a blanc version. no changes will be safed here. if you open in Firefox about:support there ist the real Path of the Profile.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import os
#Start Browser
option = Options()
#set options as u like, for example:
option.log.level = "warn"
option.set_preference("browser.link.open_newwindow", 3)
option.set_preference("browser.link.open_newwindow.restriction", 0)
#load profile
cwd = os.getcwd()
pfadffprofile = cwd+"\\"+"ffprofile.selenium"
ffprofile = webdriver.firefox.firefox_profile.FirefoxProfile(profile_directory = pfadffprofile)
driver = webdriver.Firefox(firefox_profile= ffprofile, options=option)
print("Get FF Temp Profile Path")
driver.get("about:support")
box = driver.find_element_by_id("profile-dir-box")
ffTempProfilePath = box.text
print("ffTempProfilePath: ",ffTempProfilePath)
# now do ur stuff
#copy ur stuff after use or periodically
print("safe Profile")
cwd = os.getcwd()
pfadffprofile = cwd+"\\"+"ffprofile.selenium"
print ("saving profile " + ffTempProfilePath + " to " + pfadffprofile)
os.system("xcopy " + ffTempProfilePath + " " + pfadffprofile +" /Y /G /K /R /E /S /C /H")
print ("files should be copied :/")
#close driver
driver.quit()