我已经在我的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的第一个答案
我在装有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)
我认为这里最简单的解决方案就是运行Python xvfb-run
:
sudo apt-get install xvfb
xvfb-run python <your_file_or_args>
检查您的DISPLAY
环境变量.echo $DISPLAY
在命令行中运行.
如果没有打印任何内容,那么您运行的FireFox没有分配任何DISPLAY.你应该分配一个!运行export DISPLAY=:1
运行你的Python脚本之前,在命令行.
查看此主题以获取更多信息:http://hashcat.net/forum/thread-1973.html
对于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的第一个答案
我也面临同样的问题.我使用的是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