WebDriverException:消息:连接被拒绝

时间:2017-07-23 20:49:56

标签: python selenium ubuntu geckodriver

我正在使用selenium和geckodriver尝试获取一个html页面。以下是我正在使用的代码

from pyvirtualdisplay import Display    
from selenium import webdriver  

#start the virtual display      
display = Display(visible=0, size=(800, 600))   
display.start()

# start the browser. Using firefox.
browser = webdriver.Firefox()

browser.get('https://www.google.com')

print browser.title

browser.quit()

display.stop()

当我使用python执行它时,我收到以下错误消息。

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    browser = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/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

我咨询了geckodriver.log

1500842848329   geckodriver INFO    geckodriver 0.18.0
1500842848362   geckodriver INFO    Listening on 127.0.0.1:45467
1500842849526   geckodriver::marionette INFO    Starting browser /usr/bin/firefox with args ["-marionette"]

我不确定下一步该去哪儿。任何提示?

Update1:​​似乎当我在VPS上重置电源时,脚本开始工作。我进一步调查了它,并记得有四个已经失效的进程从之前的firefox执行中运行,我不得不杀死它。我对Linux中进程处理的内部知识不够了解,无法完全理解这一点。 geckodriver会变得混乱吗?

1 个答案:

答案 0 :(得分:1)

我也有这个问题......即使在运行X服务器和功能齐全的KDE环境的机器上也是如此。事实证明,使用geckodriver和Firefox时有严格的版本要求。我使用最新版本的geckodriver运行旧版本的Firefox。一旦我下载了发行说明中列出的版本(并在我的webdriver声明中指定了firefox二进制文件的路径),它就运行良好:

driver = webdriver.Firefox(firefox_binary='/path/to/latest/firefox/binary')

https://github.com/mozilla/geckodriver/releases