如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用? [英] How to install PhantomJS for use with Python Selenium on the Raspberry Pi?

查看:22
本文介绍了如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Selenium WebDriverPhantomJS 作为我的 Raspberry Pi 上的无头浏览器运行 Python 脚本> 运行 Raspbian.

I want to run a Python script using Selenium WebDriver with PhantomJS as a headless browser on my Raspberry Pi running Raspbian.

我最初是在 OS X 中编写脚本的,它运行良好.但是在尝试使其在 Raspberry 上运行时,我遇到了问题.

I originally wrote the script in OS X where it works fine. But in trying to make it work on the Raspberry, I'm having problems.

尝试运行脚本时,出现此错误:

When trying to run the script, I get this error:

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 项目不同步).

I have seen other people have problems resembling mine – with varying solutions – most seem to involve building PhantomJS yourself, or clone and install a Github branch adapted for Raspberry (which now is out-of-sync with the main PhantomJS project).

  • 有谁知道如何解决问题——以及问题的真正含义是什么?
  • 如果解决方案涉及将二进制文件手动安装到 /usr/local/bin 等,我该怎么做?PhantomJS 网页 上的二进制文件适用于 linux-x86linux-i686,所以我假设它们不适用于 Raspberry Pi 2 B ARM Cortex A-7 处理器.
  • 我也尝试根据这些说明自己构建 PhantomJS,但过程在中途冻结.Raspberry 也不符合推荐的构建硬件要求.
  • Does anyone know how to resolve the problem – and indeed what the problem really is about?
  • If the solution involves installing the binaries manually to /usr/local/bin or such, how would I do that? The binaries available on the PhantomJS webpage is for linux-x86 and linux-i686, so I'm assuming they won't work on an Raspberry Pi 2 B ARM Cortex A-7 processor.
  • I have also tried to build PhantomJS myself according to these instructions, but the process froze midway through. The Raspberry also doesn't meet the recommended hardware requirements for building.
  • 我正在使用 Python 2.7.9
  • 我创建了一个 virtualenv 并在其中安装了所有 Python 模块;例如pip install selenium,并尝试在此处运行脚本
  • 我已经通过 sudo apt-get install phantomjs
  • 安装了最新版本的 PhantomJS
  • 我在测试时禁用了我的 ufw 防火墙
  • I am using Python 2.7.9
  • I have created a virtualenv and installed all the Python modules in it; e.g. pip install selenium, and attempt to run the script here
  • I have installed the latest version of PhantomJS via sudo apt-get install phantomjs
  • I have disabled my ufw firewall while testing

推荐答案

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

Ok I will start with the solution, there is a version compiled for arm here phantomjs-linux-armv6l, on the pi run the following commands:

$ 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 会在你的路上.

So phantomjs will be on your path.

pi@raspberrypi ~ $ phantomjs --version
1.9.0

pi@raspberrypi ~ $ phantomjs
phantomjs> 

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

Now we have that done, time to test:

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")>

来自常见问题解答.从 PhantomJS 1.5 开始,它是纯无头的,不再需要运行 X11/Xvfb..

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

I tried using xvfb-run and exporting the display, using a shell script in init.d to start xvfb, I got a little further being able to run iceweasel from bash headless no problem but still no cigar when it came to phantomjs and selenium. I think it may just come down to an incompatibility between selenium and the version of phantomjs, regardless having 1.9.0 and real headless browsing is a lot more desirable.

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

I was in the middle of setting up a toolchain and was going to try to compile myself when I found the link above, for anyone interested in cross compiling, crosstools-ng makes life a lot easier.

我正在运行 arm6,还有一个 编译版本arm7使用2.0.0,依赖项是:

I am running an arm6, there is also a compiled version for arm7 using 2.0.0, the dependencies are:

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:

The install procedure, I have extracted the binary to dropbox:

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

原github链接为phantomjs-2.0.0-armv7

这篇关于如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆