如何修复Selenium WebDriverException:浏览器似乎已经退出,无法连接? [英] How to fix Selenium WebDriverException: The browser appears to have exited before we could connect?
问题描述
我已经在centos6.4服务器上安装了firefox和Xvfb以使用Selenium Webdriver.
I have installed firefox and Xvfb on my centos6.4 server to use selenium webdriver.
但是,当我运行代码时,出现了错误.
But, when I run the code, I got an error.
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文件夹中的所有文件,所以我做到了.但是,它仍然不起作用.
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.
任何人都可以给我帮助吗?
Could anyone please give me a help?
提前谢谢!
修改
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'
推荐答案
对于Google员工来说,此答案对我不起作用,因此我不得不使用
for Googlers, this answer didn't work for me, and I had to use this answer instead. I am using AWS Ubuntu.
基本上,我需要先安装Xvfb,然后安装pyvirtualdisplay:
Basically, I needed to install Xvfb and then pyvirtualdisplay:
sudo apt-get install xvfb
sudo pip install pyvirtualdisplay
完成此操作后,此python代码将起作用:
Once I had done that, this python code worked:
#!/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的第一个答案
Thanks to @That1Guy for the first answer
这篇关于如何修复Selenium WebDriverException:浏览器似乎已经退出,无法连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!