Python Firefox Build 不适用于 Selenium
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27321907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firefox Build does not work with Selenium
提问by Thomas Müller
for my research, I did some source code modifications in firefox and build it myself. In order to automate testing, I opted to use Selenium but unfortunately, my newly built Firefox seem to not support Selenium.
为了我的研究,我在 Firefox 中做了一些源代码修改并自己构建。为了自动化测试,我选择使用 Selenium,但不幸的是,我新建的 Firefox 似乎不支持 Selenium。
I did the following:
我做了以下事情:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("/path/to/firefox/binary")
d = webdriver.Firefox(firefox_binary=binary)
d.get("http://www.google.de")
The Firefox does open and is responsive (I can enter a website in the search bar). But after a while, the python script crashes with the following error message:
Firefox 确实打开并响应(我可以在搜索栏中输入网站)。但过了一会儿,python 脚本崩溃并显示以下错误消息:
Traceback (most recent call last):
File "firefox.py", line 7, in <module>
d = webdriver.Firefox(firefox_binary=binary)
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
self.binary, timeout),
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 109, in _wait_until_connectable
raise WebDriverException("Can't load the profile. Profile "
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.
I did google that error message and most solutions suggested, that I should Update Selenium since it does not support the Firefox version used. Unfortunately, I installed the newest version of selenium (2.44.0) and I even used an older version of firefox (version 33) to rule out that point.
我确实在谷歌上搜索了该错误消息并建议了大多数解决方案,我应该更新 Selenium,因为它不支持使用的 Firefox 版本。不幸的是,我安装了最新版本的 selenium (2.44.0),我什至使用了旧版本的 firefox(版本 33)来排除这一点。
I also made sure that my code modifications are not the reason for this to crash by building a clean, unmodified firefox. Selenium doesn't work with this firefox either.
我还通过构建一个干净的、未修改的 Firefox 来确保我的代码修改不是导致崩溃的原因。Selenium 也不适用于此 Firefox。
If I don't specify a firefox binary and let Selenium use the installed Firefox, everything works fine. So my guess is, that something is wrong with the firefox build, which I did exactly as mentioned in the online documentation (e.g. ./mach build).
如果我不指定 firefox 二进制文件并让 Selenium 使用已安装的 Firefox,则一切正常。所以我的猜测是,firefox 构建有问题,我完全按照在线文档(例如 ./mach build)中提到的那样做。
Has anyone an idea, what my mistake might be? Any help is greatly appreciated!
有谁知道我的错误可能是什么?任何帮助是极大的赞赏!
Some setup information:
一些设置信息:
- Firefox 33
- Selenium 2.44.0
- Python 3.4 (also tried 2.7, doesn't work either)
- Firefox build with Ubuntu 14.04
- 火狐 33
- 硒 2.44.0
- Python 3.4(也试过 2.7,也不行)
- Firefox 使用 Ubuntu 14.04 构建
回答by Justin
I have spent a long time debugging this and ultimately gave up trying to make incompatible versions of selenium/firefox work. I just don't have the expertise in firefox to go any further. My recommendation is downloading stable versions from https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/and keep trying combinations of firefox/selenium that work for your environment. For me, this is:
我花了很长时间调试这个,最终放弃了让不兼容的 selenium/firefox 版本工作的尝试。我只是没有 Firefox 方面的专业知识,无法再进一步。我的建议是从https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/下载稳定版本,并继续尝试适合您环境的 firefox/selenium 组合。对我来说,这是:
firefox==32.0.3 selenium==2.43.0
火狐==32.0.3 硒==2.43.0
I'm referring to the changelog here: http://selenium.googlecode.com/git/java/CHANGELOGto see which versions are supposedly compatible.
我指的是此处的更改日志:http: //selenium.googlecode.com/git/java/CHANGELOG以查看据称兼容的版本。
Basically, what is happening is webdriver is polling on its port until it can establish a socket connection.
基本上,发生的事情是 webdriver 轮询其端口,直到它可以建立套接字连接。
def _wait_until_connectable(self):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# Browser has exited
raise WebDriverException("The browser appears to have exited "
"before we could connect. If you specified a log_file in "
"the FirefoxBinary constructor, check it for details.")
if count == 30:
self.kill()
raise WebDriverException("Can't load the profile. Profile "
"Dir: %s If you specified a log_file in the "
"FirefoxBinary constructor, check it for details.")
count += 1
time.sleep(1)
return True
And then if there is an socket error, keep going. So what you are probably seeing (at least what I am) is the browser hanging for 30 secs.
然后如果有一个套接字错误,继续。所以你可能看到的(至少我是这样)是浏览器挂了 30 秒。
def is_connectable(port):
"""
Tries to connect to the server at port to see if it is running.
:Args:
- port: The port to connect.
"""
try:
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_.settimeout(1)
socket_.connect(("127.0.0.1", port))
socket_.close()
return True
except socket.error:
return False
Bleh. Alright, well I finally just decided to store the specific version I want and switch to the compatible selenium version.
布莱。好吧,我终于决定存储我想要的特定版本并切换到兼容的 selenium 版本。
bin_dir = os.path.join(const.WEBDRIVER_DIR, 'firefox', 'binary', '32.0.3', 'linux-x86_64', 'firefox')
binary = FirefoxBinary(firefox_path=bin_dir)
driver = webdriver.Firefox(firefox_binary=binary)
I also highly recommend adding a log file to the firefox binary and checking that. Your issue might be unique and any bizarre errors will be logged there:
我还强烈建议向 firefox 二进制文件添加一个日志文件并检查它。您的问题可能是独一无二的,任何奇怪的错误都会记录在那里:
log_dir = os.path.join(const.LOGS_DIR, 'firefox')
try:
os.makedirs(directory)
except OSError, e:
if e.errno == errno.EEXIST and os.path.isdir(directory):
pass
log_path = os.path.join(log_dir, '{}.log'.format(datetime.datetime.now().isoformat('_'))
log_file = open(log_path, 'w')
binary = FirefoxBinary(firefox_path=bin_dir, log_file=log_file)
回答by xtremely
I suffered the same problem with FF 36.0.
我在 FF 36.0 上遇到了同样的问题。
I recommend you update selenium package to latest version with cmd 'pip install -U selenium'.
我建议您使用 cmd 'pip install -U selenium' 将 selenium 包更新到最新版本。
回答by zesaver
Ubuntu 14.04, firefox 36.0, selenium 2.44.0. The same problem, was solved by:
Ubuntu 14.04、火狐 36.0、硒 2.44.0。同样的问题,解决了:
sudo pip install -U selenium
Selenium 2.45.0 is OK with FF36.
Selenium 2.45.0 适用于 FF36。
update: Selenium 2.53+ is compatible with FF45
更新:Selenium 2.53+ 与 FF45 兼容
You can get older FF versions here
您可以在此处获取较旧的 FF 版本
回答by MisterRios
Spent an hour on this same issue. For Python3 remember to pip3
, otherwise it will only upgrade selenium on Python2, and you'll be left wondering why it's still not working.
在同样的问题上花了一个小时。对于 Python3 记得要pip3
,否则它只会在 Python2 上升级 selenium,你会想知道为什么它仍然不起作用。
sudo pip3 install -U selenium
sudo pip3 install -U selenium
回答by Mesut GUNES
For El-Capitan, the followings fixed:
对于 El-Capitan,修复了以下问题:
brew install python
brew install python
then
然后
sudo pip install --upgrade selenium
sudo pip install --upgrade selenium