首页 > 解决方案 > selenium WebDriverException:在无头 Pi 上发生“连接被拒绝”

问题描述

编辑:这似乎不是重复的,因为链接的帖子答案中的任何建议都没有修复错误。这是运行脚本后的 geckodriver.log:

1538960169585   mozrunner::runner       INFO    Running command: "/usr/bin/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.xicOi27i6laa"
1538960178656   Marionette      INFO    Listening on port 2828
^G[Child 17792] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-YKrXxr/firefox-esr-52.9.0esr/ipc/glue/MessageChannel.cpp, line 2152
[Child 17792] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-YKrXxr/firefox-esr-52.9.0esr/ipc/glue/MessageChannel.cpp, line 2152

我无法在无头 Raspberry Pi 3 B+ 上创建运行 python 3.5 的 webdriver 实例。我跑了

sudo apt-get install python-pip iceweasel xvfb pip install pyvirtualdisplay selenium

安装依赖项。但是,当我运行基本脚本来创建 selenium WebDriver 时,我会收到一条WebDriverException: Message: connection refused消息。

我的代码:

from selenium import webdriver
from pyvirtualdisplay import Display

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

profile = webdriver.FirefoxProfile()
profile.native_events_enabled = False

driver = webdriver.Firefox(profile)

错误信息:

 Traceback (most recent call last):
  File "simpletest", line 10, in <module>
    driver = webdriver.Firefox(firefox_profile=profile)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

这个来自另一个论坛的答案表明上面的代码应该可以工作。任何帮助表示赞赏。

标签: pythonseleniumraspberry-piheadlessiceweasel

解决方案


我在使用新的 Raspbery Pi 3 B+ 时遇到了同样的问题。我通过安装旧版本的 geckodriver 修复了该错误。我正在运行 Raspbian GNU/Linux 9 (stretch)。cat /etc/os-release如果您在 Raspberry Pi 上的 bash 终端中运行,您可以查找自己的操作系统版本。您将获得如下所示的输出:

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

如果您使用的是 Raspbian,那么当您安装 Firefox 时,您可能会收到 52.9.0 或更早的版本。firefox-esr -version您可以通过从 Raspberry Pi 上的 bash 终端运行来检查这一点。请注意,iceweasel 是真正的 firefox-esr。有关详细信息,请参阅https://lwn.net/Articles/676799/ 。版本 52.9.0 是 Raspbian 最新的、完全受支持的版本,即使您运行sudo apt-get updatesudo apt-get upgrade firefox-esr版本也不会改变。其实sudo apt-get upgrade firefox-esr会告诉你的firefox-esr is already the newest version (52.9.0esr-1~deb9u1)

由于无法升级 Firefox,因此需要安装旧版本的 geckodriver。在页面https://github.com/mozilla/geckodriver/releases/上,它推荐 Firefox 55.0 及更高版本和 Selenium 3.5 及更高版本用于 geckodriver v0.19.0。因此,您应该下载并安装 geckodriver v0.18.0。以下命令应该可以解决问题:

curl -O https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-arm7hf.tar.gz
tar -xzvf geckodriver-v0.18.0-arm7hf.tar.gz
sudo cp geckodriver /usr/local/bin/

sudo chmod +x /usr/local/bin/geckodriver注意:如果您收到权限错误,您可能需要运行。


推荐阅读