Python / Selenium / Firefox: 无法使用指定的配置文件路径启动Firefox

4

我尝试使用指定的配置文件启动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 - 这个目录是正确的Firefox配置文件目录,我使用firefox-bin --ProfileManager创建了它。

但是当我通过Selenium检查Firefox的about:cache页面时,它具有不同的缓存路径:

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

如果通过firefox-bin --ProfileManager运行火狐并选择配置文件,则在about:cache页面上会显示正确的路径:/Users/p2mbot/projects/test/firefox_profile 为什么Webdriver会忽略火狐的配置文件路径?使用Chrome就没有这样的问题。
2个回答

11

我花了大约2个小时(是的,我很慢)猜测为什么它不起作用。我已经找到了为什么配置文件没有保存回来。

当然,传递给 FirefoxProfile("myprofile/full/path") 的配置文件在运行时被使用,但是它没有被保存回来,因为(也许不明显)selenium是用于测试的,测试应该在没有缓存、没有任何配置文件的情况下运行,尽可能干净。

配置文件功能的目的是(也许)允许在运行测试之前安装某些扩展和设置,而不是为了保存它们。

技巧是打印出print driver.firefox_profile.path。它与通常的路径不同,名称为tmp/tmpOEs2RR/webdriver-py-profilecopy instead而不是只有tmp/tmpOEs2RR/,因此读取它会让您猜测正在使用一个配置文件。

现在,唯一剩下的事情就是把它拿回来 :)

运行这个脚本,安装一些东西,编辑一些东西,然后再次运行它 ;)

#!/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)

你可以按照该模式进行操作,或者根据你的需求简单编辑FirefoxProfilea。


这还是最新的吗?我正在尝试在Node中做同样的事情,但驱动程序没有firefoxProfile属性(或类似的任何内容)。 - Simon Meusel
@SimonMeusel 在写日期时它是工作的。顺便说一下,我现在不再使用相同的发行版或Python版本,所以我真的无法检查。关于你的firefoxProfile,可能需要将其称为FirefoxProfile,但两种方法和语法确实有所不同,而这是Python。如果您搜索nodejs问题,并且没有任何匹配您的问题,只需创建一个新的问题即可。 :-) - m3nda
这仍然有效,只有一个缺点。它也会使用您的会话。因此,如果您的上一个会话有窗口和选项卡,所有内容也将被打开。 因此,建议创建一个新配置文件,按照您的需要进行配置,并使用该配置文件,而不是使用默认/当前浏览器配置文件。 这是我发现可靠工作的唯一建议。谢谢。 - R J
@RJ 没有人说你应该使用你的个人资料。只需创建一个新的,将文件夹复制到您的项目文件夹中并开始使用它。没有更多的事情 :-). 这个问题是关于将更改保存回个人资料的。感谢确认仍然有效。 - m3nda
1
最近有人成功运行过这个吗?我正在使用FireFox 75.0和geckodriver v0.26.0。临时配置文件不保存任何更改。我尝试通过首选项GUI删除cookie并安装扩展程序。临时配置文件中的每个文件都具有相同的md5哈希值。 - Jake Hilborn

2
感谢 @m3nda,我在 Windows 和 Python 3 上找到了一个类似的可怕解决方案。正如 Jake Hilborn 所指出的那样,它不再起作用了。问题在于,driver.firefox_profile.path 似乎是临时配置文件夹,但这是一个空白版本,没有任何更改会被保存在这里。如果在 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()

这对插件设置有效吗?我已经尝试保存插件配置,但它们似乎在启动时被忽略了? - Chris Njuguna
抱歉,我从未测试过插件。 - Blu3bar0n

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接