4

I try to start firefox with specified profile:

firefox_profile = webdriver.FirefoxProfile('/Users/p2mbot/projects/test/firefox_profile')
driver = webdriver.Firefox(firefox_profile=firefox_profile)

driver.get('http://google.com')
time.sleep(60)
driver.quit()

/Users/p2mbot/projects/test/firefox_profile -- this directory is correct firefox profile dir, I created it with firefox-bin --ProfileManager

But when I check about:cache page in firefox via selenium, it has different path for cache:

Storage disk location:  /var/folders/jj/rdpd1ww53n95y5vx8w618k3h0000gq/T/tmpp2ahq70_/webdriver-py-profilecopy/cache2

If run firefox via firefox-bin --ProfileManager and choose the profile, it will show at about:cache page correct path /Users/p2mbot/projects/test/firefox_profile

Why webdriver ignore profile path for firefox? With chrome there is not such problem.

p2mbot
  • 173
  • 2
  • 8

2 Answers2

11

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.

m3nda
  • 1,986
  • 3
  • 32
  • 45
  • is this still up to date? I'm trying to do the same in node, but the driver has no firefoxProfile property (or anything similar) – Simon Meusel May 21 '17 at 15:35
  • @SimonMeusel it was working at write date. Btw, i am not anymore on the same distro nor python version so i really can't check that. About your firefoxProfile, it may be that you have to call it FirefoxProfile, but, both methods and syntax does differ from one to other, and this is Python. It's better if you search for nodejs questions, and if none matches your problem just create a new one. :-) – m3nda May 21 '17 at 19:23
  • This still works, only one downside. It would use your session too. So if your last session had windows and tabs, all would be opened too. So it is recommended to have a new profile, configure it as you would need, and use that, instead of using your default/current browser profile. This was about the only suggestion I found to be reliably working. Thanks – R J May 13 '18 at 09:05
  • @RJ No one said you should use your personal profile. Just create a new one, copy folder to your project folder and work with it. Nothing more :-). This question is all about to save changes back to profile. Thanks for confirming that still works. – m3nda May 14 '18 at 15:22
  • 1
    Has anyone gotten this to work recently? I'm using FireFox 75.0 and geckodriver v0.26.0. The temp profile does not save any changes. I tried deleting cookies via the Preferences GUI and installing an extension. Every single file in the temp profile has the same md5 hash. – Jake Hilborn Apr 19 '20 at 20:08
2

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()
Blu3bar0n
  • 23
  • 4