Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

Python / Selenium / Firefox: Can't start firefox with specified profile path

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...