Selenium的Firefox网络驱动程序在一台机器上工作,但不能在另一台机器上工作



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

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

以下是它在谷歌计算实例上引起的错误:

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

一台机器运行在谷歌计算服务器上(它出错了),另一台运行在虚拟机VirtualBox上(它正常工作),但这是我能找到的唯一区别,这无关紧要。

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

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

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

from selenium import webdriver
driver = webdriver.PhantomJS

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

正如您所指出的,原因可能是您在无头系统上运行。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());

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

相关内容

最新更新