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

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

问题描述

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

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编译的版本

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头运行iceweasel毫无头绪,但是仍然对于phantomjs和硒,没有雪茄.我认为这可能归因于硒与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.

对于正在对交叉编译感兴趣的任何人,

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,还有一个编译版本使用2.0.0的arm7,依赖关系为:

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

安装过程中,我已将二进制文件提取到保管箱:

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

The original github link is phantomjs-2.0.0-armv7

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

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