如何在树莓派上安装PhantomJS以便与Python Selenium一起使用?

9
我希望使用Selenium WebDriverPhantomJS作为无头浏览器,在运行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-x86linux-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防火墙

1
找到 phantomjs 的位置后,输入 locate phantomjs 命令,然后将其移动到 /usr/bin 目录下。在运行 locate 命令之前,您可能需要先运行 sudo updatedb 命令。此外,您可能还需要安装包含 phantomjs 的软件包。 - Joran Beasley
1
@JoranBeasley Locate 给出了以下输出结果:http://pastebin.com/BBG2wgF0 - P A N
1
你试过这个吗?https://dev59.com/D2Mk5IYBdhLWcg3w2RaS#23309447 (基本上是重新安装node和phantomjs) - Joran Beasley
1
版本为1.4,需要x11或vfvb,我现在遇到了一个与无法连接到X服务器有关的错误。我目前正在从源代码编译,已经遇到了一些问题,但我会重新开始编译,看看能否解决它。在树莓派上,这将需要几个小时,所以我明天会告诉你结果。 - Padraic Cunningham
1
最近我看到越来越多的人在使用无头Chrome和Firefox。https://stackoverflow.com/questions/49172172/need-headless-browser-for-armv7-linux-processor - Mr-Programs
显示剩余15条评论
3个回答

20

好的,我将从解决方案开始。这里有一个编译适用于 ARM 架构的版本:phantomjs-linux-armv6l,在树莓派上运行以下命令:

$ 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('https://dev59.com/-ZXfa4cB1Zd3GeqPg5Po#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开始,它是纯粹的无头浏览器,不再需要运行X11/Xvfb。
我尝试使用xvfb-run并导出显示器,在init.d中使用shell脚本启动xvfb,这样可以从bash headless轻松运行iceweasel,但在运行phantomjs和selenium时仍然无法正常工作。我认为这可能只是selenium与phantomjs版本不兼容的问题,但始终希望能够进行真正的无头浏览。
我正在设置一个工具链,并将尝试自己编译,当我发现上面的链接时,对于任何有兴趣进行交叉编译的人来说,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

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

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

原始的 Github 链接是 phantomjs-2.0.0-armv7


非常感谢您提供的出色答案、研究和解决方案!我今晚会尝试一下!这份努力值得超过50赏金,所以如果解决方案得到确认,不要惊讶我会再次向您寻求帮助 :) - P A N
1
没问题,我忘记你的是arm7了,但是有很多版本在流传,我添加了一个链接到适用于arm7的版本和安装过程,因为它是在树莓派上编译的,所以应该可以正常工作。 - Padraic Cunningham
1
我将在一分钟内添加二进制本身的链接。 - Padraic Cunningham
1
是的!非常感谢你! - P A N
-bash: /usr/bin/phantomjs: cannot execute binary file - User
使用命令 @User $ cat /proc/cpuinfo 可以查看 arm 版本,以确定是否安装了正确的版本。 - Mr-Programs

0

这是我实现它的方式:

For 64-bit system, download phantomjs-1.9.7-linux-x86_64.tar.bz2 (12.6 MB).
For 32-bit system, download phantomjs-1.9.7-linux-i686.tar.bz2 (12.9 MB).

Step 1> $ wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2

Step 2> $ tar -xvf phantomjs-1.9.7-linux-x86_64.tar.bz2

Step 3> $ cd phantomjs-1.9.7-linux-x86_64

Step 4> $ cd bin

Step 5> $ sudo cp phantomjs /usr/bin

To check phantomjs installed properly perform the following step : 

Step 6> $ phantomjs -h

To install the dependencies on Red Hat based systems:

Step 7> $ sudo yum install fontconfig freetype libfreetype.so.6 libfontconfig.so.1 libstdc++.so.6

0

下载这个phantomjs文件https://drive.google.com/open?id=1x063Krw6mZkRYW4K238a3EyRdklj5Evj

将其替换到需要的文件夹中。

赋予777权限: chmod 777 phantomjs

然后尝试使用。

对于Grafana - phantomjs文件必须位于文件夹中:/usr/share/grafana/tools/phantomjs/

这个二进制文件在我的Banana pi M3 Debian 9上工作。架构:arm armv7l。内核4.20.7-sunxi


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接