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

2024-10-01 09:17:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用指定的配置文件启动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 profile dir,我用firefox-bin --ProfileManager创建它

但是当我检查的时候关于:缓存页在firefox via selenium中,它有不同的缓存路径:

^{pr2}$

如果通过firefox bin--ProfileManager运行firefox并选择配置文件,它将显示在关于:缓存页正确路径/Users/p2mbot/projects/test/firefox_profile

为什么webdriver忽略firefox的配置文件路径?有了chrome就没有这个问题了。在


Tags: test路径getbin配置文件driverfirefoxprofile
1条回答
网友
1楼 · 发布于 2024-10-01 09:17:12

我花了大约2个小时(是的,我太慢了)猜测为什么没有工作。我找到了个人资料为什么不存回来。在

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

配置文件功能的构建(可能)是为了在运行测试之前安装某些扩展和设置,但不是为了保存它们。在

诀窍是打印出print driver.firefox_profile.path。它确实与通常的不同,它的名称是tmp/tmpOEs2RR/webdriver py profilecopy,而不仅仅是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。在

相关问题 更多 >