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

Win*_*ags 9 python linux selenium phantomjs raspberry-pi

我想在运行Raspbian的Raspberry Pi上使用Selenium WebDriverwith PhantomJS作为无头浏览器运行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
Run Code Online (Sandbox Code Playgroud)

脚本的简要版本:

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)
Run Code Online (Sandbox Code Playgroud)

我看到其他人有类似我的问题 - 有不同的解决方案 - 大多数似乎都涉及自己构建PhantomJS,或克隆并安装适用于Raspberry的Github分支(现在与主要的PhantomJS项目不同步).

问题

  • 有谁知道如何解决问题 - 事实上问题究竟是什么?
  • 如果解决方案涉及手动安装二进制文件/usr/local/bin等,我该怎么做?PhantomJS网页上提供的二进制文件用于linux-x86linux-i686,因此我假设它们不适用于Raspberry Pi 2 B ARM Cortex A-7处理器.
  • 我也试图按照这些说明自己制作PhantomJS ,但是这个过程在中途冻结了.Raspberry也不符合建议的硬件要求.

背景信息

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

Pad*_*ham 16

好的我将从解决方案开始,有一个版本为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
Run Code Online (Sandbox Code Playgroud)

我补充说:

sudo cp phantomjs-1.9.0-linux-armv6l/bin/phantomjs  /usr/bin
Run Code Online (Sandbox Code Playgroud)

所以phantomjs将在你的道路上.

pi@raspberrypi ~ $ phantomjs --version
1.9.0

pi@raspberrypi ~ $ phantomjs
phantomjs> 
Run Code Online (Sandbox Code Playgroud)

现在我们已经完成了,有时间测试:

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")>
Run Code Online (Sandbox Code Playgroud)

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

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

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

我正在运行arm6,还有一个使用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
Run Code Online (Sandbox Code Playgroud)

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

wget https://www.dropbox.com/s/epj1rji9d239dco/phantomjs
chmod +x phantomjs
sudo cp phantomjs /usr/bin
Run Code Online (Sandbox Code Playgroud)

最初的github链接是phantomjs-2.0.0-armv7