[英]Python / Selenium / Firefox: Can't start firefox with specified profile path
I try to start firefox with specified profile:我尝试使用指定的配置文件启动 Firefox:
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
/Users/p2mbot/projects/test/firefox_profile
-- 这个目录是正确的 firefox 配置文件目录,我用firefox-bin --ProfileManager
创建了它
But when I check about:cache page in firefox via selenium, it has different path for cache:但是当我通过 selenium 在 firefox 中检查 about:cache page 时,它有不同的缓存路径:
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
如果通过 firefox-bin --ProfileManager 运行 firefox 并选择配置文件,它将显示在 about:cache page 正确路径
/Users/p2mbot/projects/test/firefox_profile
Why webdriver ignore profile path for firefox?为什么 webdriver 忽略 Firefox 的配置文件路径? With chrome there is not such problem.
使用 chrome 就没有这样的问题。
I have spent around 2 hours (yes im so slow) guessing why didn't work.我花了大约 2 个小时(是的,我太慢了)猜测为什么没有用。 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.当然,传递给
FirefoxProfile("myprofile/full/path")
的配置文件在运行时使用,但是,它没有保存回来,因为(也许不明显)硒用于测试,测试应该在没有缓存的情况下运行,没有配置文件所有,尽可能干净。
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
.诀窍是打印出
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.它确实与通常的不同,名称为tmp/tmpOEs2RR/webdriver-py-profilecopy 而不仅仅是tmp/tmpOEs2RR/ ,因此阅读您应该猜测正在使用配置文件。
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.您可以根据您的需要遵循该架构或简单地编辑 FirefoxProfilea。
Thanks to @m3nda i found a simmilar and terrible solution for windows und python 3. As Jake Hilborn noticed it's not working anymore.感谢@m3nda,我为windows und python 3 找到了一个类似且糟糕的解决方案。正如Jake Hilborn 注意到的那样,它不再起作用了。 The problem is, that driver.firefox_profile.path seems to be the tmp profile direktory, but this is a blanc version.
问题是, driver.firefox_profile.path 似乎是 tmp 配置文件目录,但这是一个空白版本。 no changes will be safed here.
这里不会保护任何更改。 if you open in Firefox about:support there ist the real Path of the Profile.
如果您在 Firefox 中打开about:support有配置文件的真实路径。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.