如何修复Selenium WebDriverException:在我们连接之前,浏览器似乎已经退出了?

W3Q*_*W3Q 60 python selenium webdriver selenium-webdriver

我已经在我的centos6.4服务器上安装了firefox和Xvfb来使用selenium webdriver.

但是,当我运行代码时,我收到了一个错误.

from selenium import webdriver
browser = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

错误

selenium.common.exceptions.WebDriverException: Message: 
'The browser appears to have exited before we could connect. The output was: None'
Run Code Online (Sandbox Code Playgroud)

我在stackoverflow上读了一些相关的页面,有人建议删除tmp文件夹中的所有文件,所以我做到了.但是,它仍然无效.

有人可以帮我一个忙吗?

先感谢您!

编辑

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 64, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 103, in _wait_until_connectable
    self._get_firefox_output())
selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited     before we could connect. The output was: None' 
Run Code Online (Sandbox Code Playgroud)

Dav*_*djb 75

对于Google员工,这个答案对我不起作用,我不得不使用这个答案.我正在使用AWS Ubuntu.

基本上,我需要安装Xvfb然后pyvirtualdisplay:

sudo apt-get install xvfb
sudo pip install pyvirtualdisplay
Run Code Online (Sandbox Code Playgroud)

一旦我完成了这个,这个python代码工作:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
print browser.page_source

browser.close()
display.stop()
Run Code Online (Sandbox Code Playgroud)

感谢@ That1Guy的第一个答案


小智 26

我在装有Jenkins和xvfb的(无头)Ubuntu 14.04服务器上遇到了这个问题.我已经安装了最新的稳定的Firefox(47),它启动了一个构建失败,它运行了一个python脚本,它使用了Firefox驱动程序for selenium(版本2.53).

显然Firefox 47+与Selenium 2.53中使用的驱动程序不兼容,而Selenium 3+将使用名为"Marionette"或"Gecko Driver"的新驱动程序(尚未正式发布).

这个页面用几种语言解释了如何使用新驱动程序:https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

基本上:

  1. 从github上的项目中获取/构建可执行文件:https://github.com/mozilla/geckodriver/releases(并确保它的perms设置为可执行文件,IE chmod a+x /path/to/geckdriver-executable)
  2. 将二进制文件重命名/复制到"连线"
  3. 确保将二进制文件的位置添加到构建在执行selenium测试时使用的PATH中
  4. 更新selenium测试以使用新驱动程序

对于Python,步骤4对我来说类似于以下内容:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'

driver = webdriver.Firefox(capabilities=firefox_capabilities)
Run Code Online (Sandbox Code Playgroud)


小智 22

我也面临同样的问题.我使用的是Firefox 47和Selenium 2.53; 我将Firefox降级为45.这很有用.

  1. 首先删除Firefox 47:

    sudo apt-get purge firefox
    
    Run Code Online (Sandbox Code Playgroud)
  2. 检查可用版本:

    apt-cache show firefox | grep Version
    
    Run Code Online (Sandbox Code Playgroud)

    它将显示可用的Firefox版本,如:

    版本:47.0 + build3-0ubuntu0.16.04.1
    版本:45.0.2 + build1-0ubuntu1

  3. 安装特定版本

    sudo apt-get install firefox=45.0.2+build1-0ubuntu1
    
    Run Code Online (Sandbox Code Playgroud)
  4. 接下来,您不必再次升级到新版本.

    sudo apt-mark hold firefox
    
    Run Code Online (Sandbox Code Playgroud)
  5. 如果您想稍后升级

    sudo apt-mark unhold firefox
    sudo apt-get upgrade
    
    Run Code Online (Sandbox Code Playgroud)


小智 11

检查您的DISPLAY环境变量.echo $DISPLAY在命令行中运行.

如果没有打印任何内容,那么您运行的FireFox没有分配任何DISPLAY.你应该分配一个!运行export DISPLAY=:1运行你的Python脚本之前,在命令行.

查看此主题以获取更多信息:http://hashcat.net/forum/thread-1973.html


Phi*_*zou 5

我认为这里最简单的解决方案就是运行Python xvfb-run:

sudo apt-get install xvfb
xvfb-run python <your_file_or_args>
Run Code Online (Sandbox Code Playgroud)


小智 -7

可以通过更改输出文件(或程序的相关文件)的文件权限来解决。
我使用了 Firefox 的网络驱动程序。

尝试:

chmod -R 777 output_file
Run Code Online (Sandbox Code Playgroud)

这解决了我和你一样的麻烦。