通过geckodriver的Selenium Firefox 56 webdriver导致错误:无法连接到服务

时间:2017-10-23 17:19:08

标签: python python-3.x selenium geckodriver

我正在使用geckodriver for Firefox,而我正在使用Selenium运行Python 3。这是我在script.py文件中的代码:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')

一旦我用我的终端python3 script.py运行它,它会启动Firefox但只是不加载页面,也不会在地址栏中插入链接。我的geckodriver安装在以下位置:/usr/local/bin/geckodriver

等了大约一分钟后,我收到以下错误:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 154, in __init__
    keep_alive=True)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

我已经搜索了很多修复程序,但没有一个能够修复。

在我意识到之后,我使用的是Firefox ESR而不是正常的,我切换到正常的Firefox版本并遇到了这个问题:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    driver = webdriver.Firefox(executable_path=r'/root/Downloads/firefox-56.0.1/firefox/firefox')
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
    self.service.start()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 102, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /root/Downloads/firefox-56.0.1/firefox/firefox

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的Selenium / Geckodriver / Browser?

无论如何,你设置executable_path

driver = webdriver.Firefox(executable_path=r'pathTo/geckodriver')

修改

如果您正在使用:

  • Selenium v​​ersion = 3.6.0
  • Geckodriver version = 0.19.0
  • Firefox版本= 52.4.0

你的问题是Firefox。从GeckoDriver releases开始,建议 Firefox 55.0(及更高版本)