python - 如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用?

标签 python linux selenium raspberry-pi phantomjs

我想在我的 Raspberry Pi 上使用 Selenium WebDriverPhantomJS 作为 headless 浏览器运行 Python 脚本 运行 Raspbian。

我最初是在 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 webpage 上可用的二进制文件适用于 linux-x86linux-i686,所以我假设它们不能在 Raspberry Pi 2 B ARM Cortex A-7 处理器上工作.
  • 我也尝试根据这些 instructions 自己构建 PhantomJS ,但是过程中途卡住了。 Raspberry 也不符合推荐的构建硬件要求。

背景信息

  • 我正在使用 Python 2.7.9
  • 我创建了一个 virtualenv 并在其中安装了所有 Python 模块;例如pip install selenium,并尝试在此处运行脚本
  • 我已经通过 sudo apt-get install phantomjs 安装了最新版本的 PhantomJS
  • 我在测试时禁用了我的 ufw 防火墙

最佳答案

好的,我将从解决方案开始,这里有一个为 arm 编译的版本 phantomjs-linux-armv6l , 在 pi 上运行以下命令:

$ cd /tmp
$ wget https://github.com/aeberhardo/phantomjs-linux-armv6l/archive/master.zip
$ unzip master.zip
$ cd phantomjs-linux-armv6l-master
$ bunzip2 *.bz2 && tar xf *.tar

我补充说:

sudo cp phantomjs-1.9.0-linux-armv6l/bin/phantomjs  /usr/bin

因此 phantomjs 将在您的道路上。

pi@raspberrypi ~ $ phantomjs --version
1.9.0

pi@raspberrypi ~ $ phantomjs
phantomjs> 

现在我们已经完成了,是时候测试了:

pi@raspberrypi ~ $ cat test.py
#!/usr/bin/python
from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('http://stackoverflow.com/questions/36314771/how-to-install-phantomjs-for-use-with-python-selenium-on-the-raspberry-pi/36388824#36388824')
a = driver.find_element_by_xpath('//*[@id="question-header"]/h1/a')
print(a.text)
print(driver)
pi@raspberrypi ~ $ python test.py 
How to install PhantomJS for use with Python Selenium on the Raspberry Pi?
<selenium.webdriver.phantomjs.webdriver.WebDriver (session="b184e110-f9c4-11e5-aede-7f5c42f062d7")>

来自faq . 从 PhantomJS 1.5 开始,它是纯 headless 的,不再需要运行 X11/Xvfb。

我尝试使用 xvfb-run 并导出显示,使用 init.d 中的 shell 脚本启动 xvfb,我进一步能够从 bash headless 运行 iceweasel 没问题,但仍然当谈到 phantomjs 和 Selenium 时,不要抽雪茄。我认为这可能只是因为 selenium 和 phantomjs 版本之间的不兼容,不管有 1.9.0 还是真正的 headless 浏览更可取。

当我找到上面的链接时,我正在设置工具链并打算自己编译,对于任何对交叉编译感兴趣的人,crosstools-ng让生活变得更轻松。

我运行的是 arm6,还有一个 compiled version对于使用 2.0.0 的 arm7,依赖项是:

sudo apt-get install flex bison gperf ruby perl libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev libpng-dev libjpeg-dev python libX11-dev libxext-dev

安装过程,我已经将二进制文件解压缩到 dropbox:

wget https://www.dropbox.com/s/epj1rji9d239dco/phantomjs
chmod +x phantomjs
sudo cp phantomjs /usr/bin

原来的github链接是phantomjs-2.0.0-armv7

关于python - 如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36314771/

相关文章:

python - 朴素贝叶斯多项式模型

c++ - 确定不必要地链接了哪些静态库

c# - 具有相似元素的 Selenium Webdriver C# 页面

linux - Linux上的视频 session 服务器

selenium - Yii 功能测试的正确路由

java - 如何从所有按钮打印文本?

python - 无法更改系统默认的 SQLite 二进制文件

python - 如何按python pandas中的值范围列表进行分组

python - 如何加快 Pandas rolling_sum() 的速度?

php - 无法在 php 中从 exec 或 shell_exec 执行 aws cli 命令