文档章节

installing phantomjs and casperjs on ubuntu

Airship
 Airship
发布于 2017/07/24 20:15
字数 1024
阅读 10
收藏 0

installing phantomjs and casperjs on ubuntu

PhantomJS is a “headless WebKit scriptable with a JavaScript API”, with CasperJS building on it to give high-level functions.

PhantomJS is used quite a lot in headless browser testing and similar tasks. I’ve looked over various projects which deal with screenshots, and it’s been on my TODO-list to add responsive screenshotting into my build process. I was kicked into gear reading the following tutorial which gives a barebones script. It also used CasperJS, which is why the script that I made (based on their script) uses it.

Another reason is because I looked closer at CasperJS and noticed that it (at least in theory) would also work with another headless browser using Gecko, SlimerJS, which means that a future iteration will theoretically be able to support both WebKit and Gecko screenshots. I have not yet tested this theory, which is why SlimerJS is not touched on in this article.

prerequisites

  • Python 2.6 or higher, which is pre-installed on Ubuntu
  • Git

install phantomjs

Instructions can be found on their page or if you feel confident around the terminal, follow the commands below (note, it’s using the version that’s the most current of the writing, 1.9.1, so if you want to ensure you have the newest, check the webpage and just replace 1.9.1 with whatever is the current newest). The one thing you should not do issudo apt-get install phantomjs, as the version in the repositories is 1.4, and as we’ve established, the latest version now is 1.9.

cd ~/Downloads #default folder for downloads, duh

Decide if you need the 64-bit or 32-bit download, where the difference in file name is that 64-bit has the suffix-x86_64.tar.bz2 and 32-bit has the suffix -i686.tar.bz2.

#64-bit, v. 1.9.1
wget http://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2

#32-bit, v. 1.9.1
wget http://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-i686.tar.bz2

#Unpack the tarball!
#64-bit, v. 1.9.1
tar -xvf phantomjs-1.9.1-linux-x86_64.tar.bz2

#32-bit, v. 1.9.1
tar -xvf phantomjs-1.9.1-linux-i686.tar.bz2

This is where there are two different paths to take. The binary bin/phantomjs is ready to use, so you can

  1. Just move it and delete the rest of the files once you clear your Download folder the next time or
  2. Move the entire folder somewhere (say the Repositories folder) and symlink the relevant binary

In case you haven’t caught this nice little tip before: Write the first few letters of the path- or filename and presstab for autocomplete, if you’re not just copy/pasting.

sidenote, ~/bin vs /usr/local/bin

I like to use the home-folder to store things like binaries, because it makes it a lot easier for me to backup everything I need the next time I need to reinstall, which is why I’m writing this with the assumption that you will put the binaries in ~/bin. I will then store most source packages under /home/user/repositories. Another reason is that I prefer to not need to use sudo on normal commands.

If you prefer another way, or need to share packages between several users on one computer, you may want to replace ~/bin with /usr/local/bin, and ~/repositories with /usr/local/src, but then you need to ensure that you are prepending the commands with sudo, as the /usr/local/ folder is owned by root.

path 1

#If you don't have bin under your home folder, create it
mkdir ~/bin

#64-bit
mv phantomjs-1.9.1-linux-x86_64/bin/phantomjs ~/bin/phantomjs

#32-bit
mv phantomjs-1.9.1-linux-i686/bin/phantomjs ~/bin/phantomjs

path 2

This will just be shown using the 64-bit, but as always prior: replace x86_64 with i686 to instead use the 32-bit one.

#If it does not exist, create ~/repositories
mkdir ~/repositories

#Move it and rename
mv phantomjs-1.9.1-linux-x86_64 ~/repositories/phantomjs

Create a symbolic link to your local bin folder (creating it under your home first, see path 1) ln -sf ~/repositories/phantomjs/bin/phantomjs ~/bin/phantomjs

test it!

phantomjs --version

If you get an error that it’s not installed, close the terminal and open a new one. You might be able to just run the command . ~/.bashrc which reloads the file that appends your /home/user/bin folder to your PATH. If you’re still out of luck, google whatever error message you’re getting. It’s always a good practice when you’re running into issues.

installing casperjs

Unlike PhantomJS, you have to take the “second path” of cloning the repository and then symlinking it. The instructions are more or less taken from the installation information, with some changes in where to store things.

#If it does not exist, create ~/repositories
mkdir ~/repositories
cd ~/repositories

#clone the repository from the master branch
git clone git://github.com/n1k0/casperjs.git

#Symlink the relevant file into your bin
ln -sf ~/repositories/casperjs/bin/casperjs ~/bin/casperjs

Now test it. Assuming everything worked, you’ll get a nice helpmessage, as well as which PhantomJS version you’re using. casperjs

In case it tells you that it can’t find the file, try running the command python ~/bin/casperjs. That’s what happened when I ran it on my working machine. On the test machine (a virtual 12.04LTS Ubuntu) these commands worked without a hitch.

What I found out was the issue is that somewhere in there, the line endings had ended up screwed up, using DOS line endings where it should be using Unix. What worked for me (and is thus a good start for you) is that beautiful command dos2unix.

sudo apt-get install dos2unix #installs it if it isn't already installed
dos2unix ~/repositories/casperjs/bin/casperjs #Can't be ran on a linked resource
casperjs #Check to make sure you get said helpmessage

finishing comments

By now you should have both PhantomJS and CasperJS, which will be relevant in future articles, in particular on my workflow.

Posted 00:37

By Marie

For the moment comments are not enabled, but feel free to reach out on Twitter.

本文转载自:http://mariehogebrandt.se/articles/installing-phantomjs-and-casperjs-on-ubuntu/

共有 人打赏支持
Airship
粉丝 38
博文 875
码字总数 18996
作品 0
南京
高级程序员
zg手册 之 scrapy 开发(4)-- javascript 动态页面的抓取

javascript 动态页面 目前许多网站大量运用js脚本进行一些页面的处理,这些页面的抓取对爬虫是个挑战。 这类页面的抓取,我用到了下面的方法 分析页面(firebug/chrome调试工具 等),找到a...

东昕
2014/04/02
0
2
Web 自动化测试与智能爬虫利器:PhantomJS 简介与实战

估计部分同学没听过这个工具,那先简单介绍下它的背景与作用。 1、PhantomJS 是什么? PhantomJS是一个基于WebKit的服务器端JavaScript API,它无需浏览器的支持即可实现对Web的支持,且原生...

大数据之路
2015/04/28
0
2
使用scrapy的定制爬虫-第三章-爬虫的javascript支持

-.-编辑中.我语文是数学老师教的... 后续补充参考代码,链接. 很多网站都使用javascript...网页内容由js动态生成,一些js事件触发的页面内容变化,链接打开.甚至有些网站在没有js的情况下根本不...

斑ban
2013/04/04
0
4
修复casperjs中require自定义module传入绝对路径不能识别的问题

Notice:此问题只在windows中出现。 问题现象: 比如你在casperjs脚本中引入一个模块这样写: var util = require("d:/my_modules/util");.... 执行时就会报找不到module,d:/my_modules/util...

bobshi
2013/05/11
0
0
WEB 自动化测试工具 Selenium 简介及其应用

最近我们要对站点的指定 url 测试其页面请求完整性,并分析每个请求参数是否正常。如果不使用自动化测试工具,你也可以人肉点击、查看每个页面是否有请求遗漏和每个请求参数是否正常。当然也...

大数据之路
2014/05/23
0
0

没有更多内容

加载失败,请刷新页面

加载更多

python:关于读取文件的指定行的问题

先来造一个文件:就叫做 test.txt吧,内容是下面这个样子: 表头1,数据12,数据23,数据34,数据45,数据56,数据67,数据7 那么我们并不打算把这个表头给读出来 怎么办呢? 先来打开文...

Oh_really
21分钟前
1
0
Rails 用现代 Rails 逃离单页面应用 “兔子洞”

在工作共总是觉得turbolinks非常爽,但是却总是被说成是过时的技术,大家都喜欢spa,哪怕不用的spa的人也是禁用掉的多,找不到很好的理由劝说别人使用,这篇文章说的很到位,或者说至少是牛人...

wmzsonic
25分钟前
0
0
Hive 分布式搭建,Spark集成Hive记录

本帖详细介绍搭建步骤,仅仅记录自己搭建过程以及采坑经历。 前提环境: Hadoop集群 版本2.7.2 Spark集群 版本2.1.0 Linux版本 Centos7 准备搭建 MySql版本5.5.61 ,Hive-2.1.0 去官网下载M...

我爱春天的毛毛雨
28分钟前
2
0
打包QML程序

1、windeployqt执行路径(D:\Qt\5.12.0\msvc2017_64\bin)加入到PATH中 2、使用Qt自带的命令行交互 Command 终端(Qt 5.12.0 64-bit for Desktop (MSVC 2017))切换到 Release 编译成功的exe...

渣渣曦
今天
4
0
优秀互联网高级测试工程师应该具备的能力

概述 在之前写的互联网高级测试工程师至少具备的能力一文中,提到了测试工程师至少具备的能力,但是并没有提到优秀测试工程师应该具备的能力,下文简单的谈一谈。当然这些全部都是我的个人理...

Sam哥哥聊技术
今天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部