selenium.common.exceptions.WebDriverException:消息:连接被拒绝 [英] selenium.common.exceptions.WebDriverException: Message: connection refused
问题描述
这是我的代码:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')
browser.close()
当我运行此脚本时,它启动了firefox浏览器,但页面空白,然后命令行显示错误消息:
It launched the firefox browser when I ran this script, but the page is blank, then the command line shows the error message:
Traceback (most recent call last):
File "ad.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 76, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
我的python版本是 2.7.3 ,硒版本是 selenium-3.0.0.b3.egg-info
My python vesion is 2.7.3 and the selenium version is selenium-3.0.0.b3.egg-info
请,我该如何解决问题...
Please, how do I solve the problem ...
推荐答案
检查您的geckodriver.log
文件(应与python文件位于同一目录中)
Check your geckodriver.log
file (should be in the same directory as python file)
如果显示Error: GDK_BACKEND does not match available displays
,则安装pyvirtualdisplay:
If it says Error: GDK_BACKEND does not match available displays
then install pyvirtualdisplay:
pip install pyvirtualdisplay selenium
您可能也需要xvfb:
You might need xvfb too:
sudo apt-get install xvfb # Debian
sudo yum install Xvfb # Fedora
然后尝试添加以下代码:
Then try adding this code:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
完整示例:
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.python.org')
browser.close()
这篇关于selenium.common.exceptions.WebDriverException:消息:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!