python - 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
这个来自另一个论坛的答案表明上面的代码应该可以工作。任何帮助表示赞赏。
解决方案
我在使用新的 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 update
或sudo 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
注意:如果您收到权限错误,您可能需要运行。
推荐阅读
- mariadb - 如果写入非复制数据库,MariaDB (10.5.8) Galera 集群节点的 GTID 不同步
- python - 正则表达式:仅提取单词前的数字
- java - 根据 JSP 中其他下拉列表的选定值动态填充下拉列表
- angular - ViewChild 在浏览器背面未定义
- python - dash.Dash 的属性是什么?
- c# - 为什么我不能将资源链接到位图?
- php - PHP容器连接到外部数据库
- hybris - 在初始化期间跳过 hybris 扩展。如何调试和解决?
- javascript - ChartJS - 折线图 - 顶部/右侧的额外填充以添加额外的线条
- python - Python:将熊猫系列转换为数组并保留索引