小编典典

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

selenium

我想运行使用Python脚本Selenium WebDriverPhantomJS作为我的一个无头的浏览器 树莓派
运行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网页linux-x86linux-i686,所以我假设他们不会上树莓裨2 B的工作 的ARM Cortex A-7处理器
  • 我也尝试根据这些说明自己构建PhantomJS ,但过程中途停滞。Raspberry还不符合建议的构建硬件要求。

背景资料

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

阅读 503

收藏
2020-06-26

共1个答案

小编典典

好的,我将从解决方案开始,这里是phantomjs-linux-
armv6l
为arm编译的版本,在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")>

常见问题解答从PhantomJS 1.5开始,它是无头的,并且不再需要运行X11
/ Xvfb。

我尝试使用 xvfb运行
并导出显示,并使用init.d中的shell脚本启动xvfb,我可以使用bash来运行iceweasel毫无问题,但在phantomjs和硒方面仍然没有雪茄。我认为这可能归因于硒和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

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

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

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

2020-06-26