首页 文章

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

提问于
浏览
9

我想在运行Raspbian的_2856771上使用 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 防火墙

3 回答

  • 0

    好的我将从解决方案开始,有一个为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运行iceweasel无头没有问题,但是当它来到phantomjs和selenium时仍然没有雪茄 . 我认为它可能只是归结为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

  • 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
    
  • 16

    下载此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

相关问题