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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26070834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:01:19  来源:igfitidea点击:

How to fix Selenium WebDriverException: The browser appears to have exited before we could connect?

pythonseleniumselenium-webdriverwebdriver

提问by W3Q

I have installed firefox and Xvfb on my centos6.4 server to use selenium webdriver.

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

But, when I run the code, I got an error.

但是,当我运行代码时,出现错误。

from selenium import webdriver
browser = webdriver.Firefox()

Error

错误

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

I read some related pages on stackoverflow and someone suggested to remove all files in tmp folder, so I did it. But, it still doesn't work.

我在stackoverflow上阅读了一些相关页面,有人建议删除tmp文件夹中的所有文件,所以我做到了。但是,它仍然不起作用。

Could anyone please give me a help?

有人可以帮我吗?

Thank you in advance!

先感谢您!

Edit

编辑

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' 

回答by ZodEnIX

It can be solved by changing the file permission of the output file ( or related files to the program).
I used Firefox's webdriver.

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

Try:

尝试:

chmod -R 777 output_file

This solved me the same trouble you have.

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

回答by melchtheitroad55

I fixed this by running a recursive chown against not only the python script using selenium, but against the entire virtualenv that script was running in. I changed the ownership to the user running the file. After that, this error went away.

我通过不仅针对使用 selenium 的 python 脚本,而且针对运行脚本的整个 virtualenv 运行递归 chown 来解决这个问题。我将所有权更改为运行文件的用户。之后,这个错误就消失了。

回答by Bilal Naqvi

Check your DISPLAYenvironment variable. Run echo $DISPLAYin the command line.

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

If nothing is printed, then you are running FireFox without any DISPLAY assigned. You should assign one! Run export DISPLAY=:1in the command line before running your python script.

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

Check this thread for more information: http://hashcat.net/forum/thread-1973.html

检查此线程以获取更多信息:http: //hashcat.net/forum/thread-1973.html

回答by Davidjb

for Googlers, this answer didn't work for me, and I had to use this answerinstead. I am using AWS Ubuntu.

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

Basically, I needed to install Xvfb and then pyvirtualdisplay:

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

sudo apt-get install xvfb
sudo pip install pyvirtualdisplay

Once I had done that, this python code worked:

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

Thanks to @That1Guy for the first answer

感谢@That1Guy 的第一个回答

回答by Philip Tzou

I think the simplest solution here is just run Python with xvfb-run:

我认为这里最简单的解决方案是使用xvfb-run以下命令运行 Python :

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

回答by ayoub laaziz

update your selenuim version ---> pip install -U selenium

更新您的 selenuim 版本 ---> pip install -U selenium

回答by Joel Kleier

I was running into this on an (headless) Ubuntu 14.04 server with Jenkins and xvfb installed. I had installed the latest stable Firefox (47) which started a build failing that ran a python script which used the Firefox driver for selenium (version 2.53).

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

Apparently Firefox 47+ is not compatible with the driver used in Selenium 2.53, and Selenium 3+ will be using a new driver called "Marionette" or "Gecko Driver" (which isn't officially released yet).

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

This page explains how to use the new driver pretty well, in several languages: https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

此页面以多种语言解释了如何很好地使用新驱动程序:https: //developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

Basically:

基本上:

  1. get/build the executable from the project on github: https://github.com/mozilla/geckodriver/releases(and make sure it's perms are set to be executable, IE chmod a+x /path/to/geckdriver-executable)
  2. rename/copy binary to "wires"
  3. make sure the binary's location is added to the PATH that the build uses when executing the selenium test
  4. update the selenium test to use the new driver
  1. 从 github 上的项目获取/构建可执行文件:https: //github.com/mozilla/geckodriver/releases(并确保将其 perms 设置为可执行文件,IE chmod a+x /path/to/geckdriver-executable
  2. 重命名/复制二进制文件到“电线”
  3. 确保将二进制文件的位置添加到构建在执行 selenium 测试时使用的 PATH
  4. 更新 selenium 测试以使用新驱动程序

For Python, step 4 looked something like the following for me:

对于 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)

回答by Jacob George

Rollback your Firefox to the previous working version. I suggest 2 versions back. Disable Firefox Maintenance Service.

将您的 Firefox 回滚到以前的工作版本。我建议返回 2 个版本。禁用 Firefox 维护服务。

I was working on a solution and the Firefox Maintenance Service updated Firefox to the latest build in the background. This broke my code and it was giving me this error.

我正在研究一个解决方案,Firefox 维护服务在后台将 Firefox 更新到最新版本。这破坏了我的代码,它给了我这个错误。

Now it is fixed!

现在它是固定的!

Thank you everyone!

谢谢大家!

回答by Amogh Joshi

I too had faced same problem. I was on Firefox 47 and Selenium 2.53; I downgraded Firefox to 45. This worked.

我也遇到过同样的问题。我使用的是 Firefox 47 和 Selenium 2.53;我将 Firefox 降级到 45。这有效。

  1. Remove Firefox 47 first :

    sudo apt-get purge firefox
    
  2. Check for available versions:

    apt-cache show firefox | grep Version
    

    It will show available firefox versions like:

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

  3. Install a specific version

    sudo apt-get install firefox=45.0.2+build1-0ubuntu1
    
  4. Next you have to not upgrade to the newer version again.

    sudo apt-mark hold firefox
    
  5. If you want to upgrade later

    sudo apt-mark unhold firefox
    sudo apt-get upgrade
    
  1. 首先删除 Firefox 47:

    sudo apt-get purge firefox
    
  2. 检查可用版本:

    apt-cache show firefox | grep Version
    

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

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

  3. 安装特定版本

    sudo apt-get install firefox=45.0.2+build1-0ubuntu1
    
  4. 接下来,您不必再次升级到较新的版本。

    sudo apt-mark hold firefox
    
  5. 如果以后想升级

    sudo apt-mark unhold firefox
    sudo apt-get upgrade
    

回答by Ashok Devatwal

This error is due to your Xvfb is not running. So restart your xvfb:

此错误是由于您的 Xvfb 未运行。所以重启你的 xvfb:

Xvfb :99 -ac

then check. This works for me.

然后检查。这对我有用。