硒的火狐webdriver在一台机器,但不是另一台 [英] Selenium's firefox webdriver works in one machine but not another
问题描述
这是我正在运行的非常简单的代码:
来自selenium import webdriver
driver = webdriver。 Firefox()`
以下是在Google计算实例中导致的错误:
pre code $ traceback最近一次调用最后一次
在<
文件/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py,第77行,在__init__
self.binary,timeout),
文件/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py,第49行,在__init__
self.binary.launch_browser(self.profile)
文件/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py,第68行,在launch_browser
self._wait_until_connectable()
文件/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py,第98行,在_wait_until_connectable
中引发WebDriverException(浏览器似乎退出
selenium.common.exceptions.WebDriverException:消息:在我们可以连接之前,浏览器似乎已经退出了,如果你在FirefoxBinary构造
中指定了一个log_file,或者检查它的细节
$ c
$ b
在两台机器上,我都有
-
Python 2.7.6
-
Mozilla Firefox 42.0
- code> locate selenium 返回
... /usr/local/lib/python2.7/dist-packages/selenium-2.48.0.dist-info/DESCRIPTION .rs ...
- 因此,
Selenium 2.48.0
-
lsb_release -a
没有LSB模块可用。
发行编号:Ubuntu
描述:Ubuntu 14.04.3 LTS
发行:14.04
Codename:trusty
一个机器运行在谷歌计算服务器(它的错误),一个是在一个VM的VirtualBox(它的工作),但这是唯一的区别,我可以找到它和它不要紧。
还有什么其他的区别可能会导致这个错误在一台机器上,而不是另一台?
我想也许谷歌计算引擎不能打开浏览器窗口,因为你只能ssh到命令行?那么这个问题不能解决?
$ b 注意:
此代码适用于两台机器:
from selenium import webdriver
driver = webdriver.PhantomJS
但是,我需要使用firefox,所以这不是一个解决方案,只需要记住另一件事。
解决方案
正如你所指出的,原因可能是你正在无头系统上运行。 PhantomJS和HTMLUnit和这样的stuf不需要有一个x服务器。
你可以尝试在命令行上启动firefox,只需键入火狐
。
如果失败,例如异常无法在0.0
或smth上找到/打开显示。像这样,你应该使用XVFB:
下面是描述如何使用XVFB。
sudo apt-get update $ b $ sudo apt-get install firefox xvfb
sudo Xvfb:10 -ac $ b $ export DISPLAY =:10
$ c $现在你可以尝试用 firefox
启动firefox
我复制的命令:
http://www.installationpage.com/selenium/how-to-run-selenium-headless-firefox-in-ubuntu/
如果你想在你的java应用程序中设置DISPLAY Port,并且只是为了你的firefox实例,你可以这样做:
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.setEnvironmentProperty(DISPLAY,:10);
新的FirefoxDriver(firefoxBinary,新的FirefoxProfile());
Here is the very simple code that I am running:
from selenium import webdriver
driver = webdriver.Firefox()`
Here is the error that it causes on a google compute instance:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 77, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 49, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary construct
or, check it for details.
On both machines I have
Python 2.7.6
Mozilla Firefox 42.0
locate selenium
returns ... /usr/local/lib/python2.7/dist-packages/selenium-2.48.0.dist-info/DESCRIPTION.rs ...
- So,
Selenium 2.48.0
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
One machine is run on a google compute server (it errors) and one is on a VM VirtualBox (it works), but this is the only difference that I can find and it shouidn't matter.
What other differences are there that could cause this error on one machine but not the other?
NOTE:
I am thinking maybe the google compute engine cannot open a browser window since you can only ssh into a command line? Then this problem can't be solved?
NOTE:
This code works on both machines:
from selenium import webdriver
driver = webdriver.PhantomJS
But, I need to use firefox, so this is not a solution just another thing to keep in mind.
解决方案 As you noted the reason could be that you are running on an headless system. PhantomJS and HTMLUnit and stuf like this don't requiere to have a x-server.
Can you try to start firefox on your commandline just typing firefox
.
If that fails with an exception like Can't find/open display on 0.0
or smth. like this, you should use XVFB:
Here is a description how to use XVFB.
sudo apt-get update
sudo apt-get install firefox xvfb
sudo Xvfb :10 -ac
export DISPLAY=:10
Now you can try to start firefox with firefox
The commands i copied from:
http://www.installationpage.com/selenium/how-to-run-selenium-headless-firefox-in-ubuntu/
If you like to set the DISPLAY Port within your java application and just for your firefox instance you can do it like this:
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.setEnvironmentProperty("DISPLAY", ":10");
new FirefoxDriver(firefoxBinary, new FirefoxProfile());
这篇关于硒的火狐webdriver在一台机器,但不是另一台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!