Selenium:FirefoxProfile 异常无法加载配置文件
- 2025-03-14 08:57:00
- admin 原创
- 11
问题描述:
根据之前的问题,我将 Selenium 更新至版本 2.0.1 但现在我遇到了另一个错误,即使配置文件存在于/tmp/webdriver-py-profilecopy
:
文件“/home/sultan/Repository/Django/monitor/app/request.py”,第 236 行,正在执行
浏览器 = Firefox(配置文件)
文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”,第 46 行,位于 __init__ 中
self.binary,超时),
文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py”,第 46 行,位于 __init__ 中
self.binary.launch_browser(self.profile)
launch_browser 中的文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py”,第 44 行
self._wait_until_connectable()
文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py”,第 87 行,位于 _wait_until_connectable 中
引发 WebDriverException(“无法加载配置文件。配置文件目录:%s”%self.profile.path)
selenium.common.exceptions.WebDriverException:无法加载配置文件。配置文件目录:/tmp/webdriver-py-profilecopy
出了什么问题?我该如何解决这个问题?
解决方案 1:
更新:
Selenium 团队在最新版本中修复了这个问题。对于几乎所有环境,修复方法如下:
pip 安装-U 硒
不清楚在哪个版本中修复了该问题(显然是r13122),但肯定在 2.26.0 (更新时的最新版本)中已经修复。
这个错误意味着_wait_until_connectable超时,因为某种原因,代码无法连接到已经加载到firefox中的webdriver扩展。
我刚刚向 selenium 报告了一个错误,之所以出现此错误,是因为我正在尝试使用代理,而配置文件中配置的 4 个更改中只有 2 个被 Firefox 接受,因此代理未配置为与扩展通信。不知道为什么会发生这种情况...
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2061
解决方案 2:
将 Ubuntu 升级到 12.04 后我遇到了同样的问题。
该问题出在软件包方面,已在最新版本的库中修复。只需更新 selenium 库即可。对于几乎所有 Python 环境,这是:
pip install -U selenium
解决方案 3:
我在使用 FF 32.0 和 Selenium selenium-2.42.1-py2.7.egg 时也遇到了同样的问题。尝试更新 selenium,但它已经是最新版本了。解决方案是将 Firefox 降级到版本 30。以下是流程:
#Download version 30 for Linux (This is the 64 bit)
wget http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/30.0/linux-x86_64/en-US/firefox-30.0.tar.bz2
tar -xjvf firefox-30.0.tar.bz2
#Remove the old version
sudo rm -rf /opt/firefox*
sudo mv firefox /opt/firefox30.0
#Create a permanent link
sudo ln -sf /opt/firefox30.0/firefox /usr/bin/firefox
这解决了所有问题,而且这种组合效果更好!
解决方案 4:
作为Jeff Hoye答案的扩展,一种更“Pythonic”的方法是webdriver.firefox.firefox_profile.FirefoxProfile
按如下方式进行子类化:
class CygwinFirefoxProfile(FirefoxProfile):
@property
def path(self):
path = self.profile_dir
# Do stuff to the path as described in Jeff Hoye's answer
return path
然后,创建你的驱动程序:
driver = webdriver.Firefox(firefox_profile=CygwinFirefoxProfile())
解决方案 5:
如果pip install -U selenium
不起作用(就我的情况而言),请尝试将 Firefox 降级到以前的版本。
我之前使用 Firefox 49.0,后来降级到 45.0,以确保 selenium 支持该版本。然后它运行正常。
以下是降级到 Firefox 45.0 的快速方法:
sudo apt-get install firefox=45.0.2+build1-0ubuntu1
希望这有帮助。
解决方案 6:
如果您从 cygwin 运行 webdriver,问题在于配置文件的路径仍为 POSIX 格式,这会使 Windows 程序感到困惑。我的解决方案是使用 cygpath 将其转换为 Windows 格式。
在此文件/方法中:selenium.webdriver.firefox.firefox_binary.launch_browser():
代替:
self._start_from_profile_path(self.profile.path)
和:
from subprocess import Popen, PIPE
proc = Popen(['cygpath','-d',self.profile.path], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
path = stdout.split('
', 1)[0]
self._start_from_profile_path(path)
#self._start_from_profile_path(self.profile.path)
由于 Python 根本不是我的主要编程语言,如果有人能推荐一种更 Python 化的方法,也许我们可以将其推入发行版。如果它在 cygwin 中开箱即用,那肯定会很方便。
解决方案 7:
我遇到了同样的问题,并认为是 selenium / Firefox 的组合错误。结果发现我的 .mozilla/ 文件夹权限只有 root 用户才能访问。这样做chmod 770 ~/.mozilla/
很有效。我建议在进一步排除故障之前确保这不是问题所在。
解决方案 8:
pip install -U selenium
我遇到了同样的问题Firefox 34.0.5 (Dec 1, 2014)
,并从升级 Selenium2.42.1
解决2.44.0
了我的问题。
但是,我后来又看到了这个问题,我想是在 2.44.0 中,另一个升级修复了这个问题。所以我想知道是否可以通过简单地卸载然后重新安装来修复它。如果是这样,我不确定这会表明潜在的问题是什么。
解决方案 9:
我使用的是 selenium 2.53 和 Firefox 版本 55.0。我通过安装旧版本的 Firefox (46.0.1) 解决了这个问题,因为 selenium 2.53 不适用于 Firefox 版本 47.0 及更高版本。
解决方案 10:
这不是一个合适的解决方案,但对我来说很有效,如果有人可以改进,我会很高兴知道。我只是以 root 身份运行我的脚本:sudo python myscript.py
。我想我可以通过更改配置文件默认文件或目录来解决问题。