Selenium的firefox webdriver可以在一台机器上运行,但不能在另一台机器上运行

时间:2015-11-06 04:46:31

标签: python firefox ubuntu selenium google-compute-engine

以下是我正在运行的非常简单的代码:

from selenium import webdriver
driver = webdriver.Firefox()`

以下是它在Google计算实例上导致的错误:

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.

我在两台机器上

  • Python 2.7.6
  • Mozilla Firefox 42.0
  • locate selenium返回... /usr/local/lib/python2.7/dist-packages/selenium-2.48.0.dist-info/DESCRIPTION.rs ...
  • 所以,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

一台机器在谷歌计算服务器上运行(它出错),一台机器在VM VirtualBox上运行(它可以运行),但这是我能找到的唯一区别,它很重要。

有哪些其他差异可能导致一台机器出现此错误而另一台机器出现此错误?

注意: 我想也许谷歌计算引擎无法打开浏览器窗口,因为你只能ssh到命令行?那么这个问题无法解决?

注意: 此代码适用于两台计算机:

from selenium import webdriver
driver = webdriver.PhantomJS

但是,我需要使用firefox,所以这不是一个解决方案,只需记住另一件事。

2 个答案:

答案 0 :(得分:1)

正如您所指出的,原因可能是您在无头系统上运行。像这样的PhantomJS和HTMLUnit以及stuf都没有要求拥有x服务器。

您是否可以尝试在命令行上启动firefox,只需输入firefox即可。 如果失败,则会出现Can't find/open display on 0.0或smth等异常。像这样,你应该使用XVFB:

以下是如何使用XVFB的说明。

sudo apt-get update
sudo apt-get install firefox xvfb
sudo Xvfb :10 -ac
export DISPLAY=:10

现在您可以尝试使用firefox

启动Firefox

我复制的命令: http://www.installationpage.com/selenium/how-to-run-selenium-headless-firefox-in-ubuntu/

如果你想在你的java应用程序中设置DISPLAY端口,只为你的firefox实例设置,你可以这样做:

    FirefoxBinary firefoxBinary = new FirefoxBinary();
    firefoxBinary.setEnvironmentProperty("DISPLAY", ":10");

    new FirefoxDriver(firefoxBinary, new FirefoxProfile());

答案 1 :(得分:0)

我需要运行'gcloud init'然后默认登录。请参阅步骤1中的文档:https://cloud.google.com/compute/docs/gcloud-compute/

相关问题