【问题标题】:Selenium's firefox webdriver works in one machine but not anotherSelenium firefox webdriver 在一台机器上工作,但在另一台机器上不工作
【发布时间】:2015-11-06 04:46:30
【问题描述】:

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

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,所以这不是一个解决方案,只是要记住的另一件事。

【问题讨论】:

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


    【解决方案1】:

    正如您所指出的,原因可能是您在无头系统上运行。像这样的 PhantomJS 和 HTMLUnit 和 stuf 不需要 x-server。

    您可以尝试在命令行上启动 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());
    

    【讨论】:

    • 我用的是python,没有用java,但是运行4行关于xvfb的代码然后打开python就可以了。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      相关资源
      最近更新 更多