【问题标题】:How to install PhantomJS for use with Python Selenium on the Raspberry Pi?如何在 Raspberry Pi 上安装 PhantomJS 以与 Python Selenium 一起使用?
【发布时间】:2016-07-18 19:22:20
【问题描述】:

我想在运行 Raspbian 的 Raspberry Pi 上使用 Selenium WebDriverPhantomJS 作为无头浏览器运行 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 webpage 上可用的二进制文件适用于 linux-x86linux-i686,所以我假设它们不适用于 Raspberry Pi 2 B ARM Cortex A-7 处理器。李>
  • 我也尝试根据这些instructions 自己构建 PhantomJS,但过程在中途冻结。 Raspberry 也不符合推荐的构建硬件要求。

背景资料

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

【问题讨论】:

  • 输入locate phantomjs 一旦你找到它移动它到/usr/bin ?您可能需要在运行 locate 之前运行 sudo updatedb ...并且您可能需要 apt-get install 它所属的任何软件包 ...
  • @JoranBeasley Locate 给出以下输出:pastebin.com/BBG2wgF0
  • 你试过这个stackoverflow.com/questions/18916123/…(基本上是重新安装node和phantomjs)
  • 版本是 1.4,需要 x11 或 vfvb,我实际上遇到了一个与无法连接到 X 服务器有关的错误,我目前正在从源代码编译,到目前为止有一些问题但回来了再次编译,所以我看看能不能把它排序,在 pi 上需要几个小时,所以明天会告诉你。
  • 我读到最近人们使用更多的无头 chrome 和 firefox stackoverflow.com/questions/49172172/…

标签: python linux selenium raspberry-pi phantomjs


【解决方案1】:

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

替换到需要的文件夹。

给 777 chmod: chmod 777 phantomjs

并尝试使用。

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

这个二进制文件适用于 Banana pi M3 Debian 9。架构:arm armv7l。内核4.20.7-sunxi

【讨论】:

    【解决方案2】:

    我是这样做的:

    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
    

    【讨论】:

      【解决方案3】:

      好的,我先从解决方案开始,这里有一个为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
      

      我补充说:

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

      来自faq从 PhantomJS 1.5 开始,它是纯无头的,不再需要运行 X11/Xvfb。.

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

      我正在建立一个工具链,当我找到上面的链接时,我正打算自己编译,对于任何对交叉编译感兴趣的人,crosstools-ng 让生活变得更轻松。

      我正在运行一个arm6,还有一个compiled version for arm7使用2.0.0,依赖项是:

      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 赏金,所以如果我在解决方案确认后向您发送更多信息,请不要感到惊讶 :)
      • 别担心,我忘了你的是 arm7 但有很多版本在敲,我添加了 arm7 版本的链接和安装过程,它应该可以正常工作,因为它是在编译的一个圆周率。
      • 我会在一分钟内添加一个指向二进制文件本身的链接
      • 是的!非常感谢!
      • -bash: /usr/bin/phantomjs: cannot execute binary file
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2015-01-26
      • 2021-12-19
      • 2021-07-28
      • 2019-09-09
      • 2018-09-13
      相关资源
      最近更新 更多