python - selenium.common.exceptions.WebDriverException : Message: connection refused

标签 python selenium selenium-webdriver

这是我的代码:

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.python.org')

browser.close()

当我运行这个脚本时它启动了 firefox 浏览器,但是页面是空白的,然后命令行显示错误消息:

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版本是selenium-3.0.0.b3.egg-info

请问我该如何解决这个问题...

最佳答案

检查你的geckodriver.log文件(应该和python文件在同一个目录)

如果它说Error: GDK_BACKEND does not match available displays 然后安装 pyvirtualdisplay:

pip install pyvirtualdisplay selenium

您可能还需要 xvfb:

sudo apt-get install xvfb # Debian

sudo yum install Xvfb # Fedora

然后尝试添加这段代码:

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()

关于python - selenium.common.exceptions.WebDriverException : Message: connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547598/

相关文章:

python - Keras ValueError : Error when checking target: expected dense_15 to have 3 dimensions, 但得到了形状为 (301390, 8) 的数组

python - Selenium 在 PC 和 RPI 上的性能差异

javascript - 使用 nightwatch.js 我如何选择具有动态提供的值的动态生成的项目

大型 json 数据下载后,Selenium Driver 落后于 ajax 调用一页

java - Selenium 上传文件时样式为 ="display:none;"

Python:解析由文本表示的长 double 组非常慢

javascript - 使用 ReactJS 访问 Flask session

python - 使用 python 在 numpy 数组中加载 tiff 堆栈

java - 在不同的浏览器 session 中运行测试,但始终得到 'NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?'

javascript - 无法在禁用和启用状态下断言双操作按钮