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

 MCphp 发布于 2022-12-19 14:41

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

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

from selenium import webdriver
browser = webdriver.Firefox()

错误

selenium.common.exceptions.WebDriverException: Message: 
'The browser appears to have exited before we could connect. The output was: None'

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

有人可以帮我一个忙吗?

先感谢您!

编辑

Traceback (most recent call last):
  File "", line 1, in 
  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' 

Davidjb.. 75

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

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

sudo apt-get install xvfb
sudo pip install pyvirtualdisplay

一旦我完成了这个,这个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()

感谢@ That1Guy的第一个答案

5 个回答
  • 我在装有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

    基本上:

      从github上的项目中获取/构建可执行文件:https://github.com/mozilla/geckodriver/releases(并确保它的perms设置为可执行文件,IE chmod a+x /path/to/geckdriver-executable)

      将二进制文件重命名/复制到"连线"

      确保将二进制文件的位置添加到构建在执行selenium测试时使用的PATH中

      更新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)
    

    2022-12-19 14:43 回答
  • 我认为这里最简单的解决方案就是运行Python xvfb-run:

    sudo apt-get install xvfb
    xvfb-run python <your_file_or_args>
    

    2022-12-19 14:43 回答
  • 检查您的DISPLAY环境变量.echo $DISPLAY在命令行中运行.

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

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

    2022-12-19 14:43 回答
  • 对于Google员工,这个答案对我不起作用,我不得不使用这个答案.我正在使用AWS Ubuntu.

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

    sudo apt-get install xvfb
    sudo pip install pyvirtualdisplay
    

    一旦我完成了这个,这个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()
    

    感谢@ That1Guy的第一个答案

    2022-12-19 14:43 回答
  • 我也面临同样的问题.我使用的是Firefox 47和Selenium 2.53; 我将Firefox降级为45.这很有用.

      首先删除Firefox 47:

      sudo apt-get purge firefox
      

      检查可用版本:

      apt-cache show firefox | grep Version
      

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

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

      安装特定版本

      sudo apt-get install firefox=45.0.2+build1-0ubuntu1
      

      接下来,您不必再次升级到新版本.

      sudo apt-mark hold firefox
      

      如果您想稍后升级

      sudo apt-mark unhold firefox
      sudo apt-get upgrade
      

    2022-12-19 14:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有