我希望使用
Selenium WebDriver
和PhantomJS
作为无头浏览器,在运行Raspbian的Raspberry Pi上运行Python脚本。
我最初在OS X中编写了这个脚本,它可以正常工作。但是在尝试在Raspberry上运行时,我遇到了问题。
当尝试运行脚本时,我会得到以下错误:
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/bin/phantomjs
脚本简介:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
user_agent = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36")
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent
serv_args = ["--ignore-ssl-errors=false", "--ssl-protocol=tlsv1",
"--disk-cache=false"]
driver = webdriver.PhantomJS(executable_path="/usr/bin/phantomjs",
desired_capabilities = dcap, service_arguments = serv_args, port=65000)
我看到其他人遇到了与我相似的问题 - 解决方案各不相同 - 大多数似乎涉及自己构建PhantomJS,或者克隆并安装适用于Raspberry的Github分支(现在与主要的PhantomJS项目不同步)。
问题
- 有人知道如何解决这个问题 - 实际上这个问题是什么?
- 如果解决方案涉及手动将二进制文件安装到
/usr/local/bin
等位置,我该怎么做?PhantomJS网页上提供的二进制文件适用于linux-x86
和linux-i686
,因此我认为它们无法在树莓派2 B ARM Cortex A-7处理器上使用。 - 我还尝试按照这些说明自行构建PhantomJS,但该过程在中途冻结。 Raspberry也不符合建议的硬件要求。
背景信息
- 我正在使用
Python 2.7.9
- 我创建了一个
virtualenv
并在其中安装了所有的Python模块;例如pip install selenium
,并尝试运行此脚本 - 我通过
sudo apt-get install phantomjs
安装了最新版本的PhantomJS - 我在测试时禁用了我的
ufw
防火墙
locate phantomjs
命令,然后将其移动到/usr/bin
目录下。在运行locate
命令之前,您可能需要先运行sudo updatedb
命令。此外,您可能还需要安装包含 phantomjs 的软件包。 - Joran Beasley