installing phantomjs and casperjs on ubuntu

发布于 2017/07/24 20:15
字数 1024
阅读 9
收藏 0
点赞 0
评论 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.


  • 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

mv phantomjs-1.9.1-linux-x86_64/bin/phantomjs ~/bin/phantomjs

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.


共有 人打赏支持
粉丝 34
博文 789
码字总数 18996
作品 0
ubuntu 安装phantomjs、chromedriver、geckodriver

一、安装phantomjs 1.从官网下载http://phantomjs.org/download.html 2.加压 tar -zxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 3.mv phantomjs-2.1.1-linux-x86_64 /usr/local/share/phanto......

littlemesieV ⋅ 04/18 ⋅ 0

[问题解决] Selenium+PhantomJS使用时报错原因及解决方案

在做动态网页爬虫时用到了selenium + PhantomJS,安装好之后运行时报错: 说是 Selenium 已经弃用 PhantomJS ,请使用火狐或者谷歌无界面浏览器。 查找之后得到答案时selenium的版本问题,使用p...

Eappo_Geng ⋅ 06/13 ⋅ 0

ELK 实验(七)Elasticsearch 集群管理

Head 插件 head插件是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序。 Github: https://github.com/mobz/elasticsearch-head git clone https://github.com/mobz/e...

pcdog ⋅ 04/20 ⋅ 0

Node/PhantomJS爬虫 - Jedi-Crawler

Jedi-Crawler 是一款轻量级 Node/PhantomJS爬虫,可以动态的抓取网页内容。 安装: npm install jedi-crawler 示例代码: var jedi = require('./modules/jedi.js'),fs = require('fs'); //...

匿名 ⋅ 2016/07/27 ⋅ 0


PhantomJS安装目录下的example简单介绍 cmd命令窗口进入相应路径下使用phantomjs xxx.js命令 这里介绍几个效果比较明显的,具体代码这里不做描述 1.colorwheel.js 会生成一个colorwheel图片 ...

anlve ⋅ 01/22 ⋅ 0

搭建 vue2 单元测试环境(karma+mocha+webpack3)


mgbq ⋅ 05/23 ⋅ 0

开源自动化测试解决方案 - AutoLine

AutoLine开源平台 AutoLine开源平台是一个开源自动化测试解决方案,基于 RobotFramework 进行二次开发,支持 RobotFramework 几乎所有的库。 特色 支持 Mac、Linux、Windows 平台 支持 Chro...

苦叶子 ⋅ 05/16 ⋅ 0


原文出处:拓海 大家好,《手把手教你写网络爬虫》连载开始了!在笔者的职业生涯中,几乎没有发现像网络爬虫这样的编程实践,可以同时吸引程序员和门外汉的注意。本文由浅入深的把爬虫技术和...

拓海 ⋅ 04/27 ⋅ 0

Vant 1.1.0 发布,有赞轻量级移动端 Vue 组件库

Vant 1.1.0 已发布,更新如下: 改进 新增 Slider 滑块组件 #897 #915 新增 BEM mixin,规范 BEM 命名格式 #921 #924 #932 #934 Cell: 优化 flex 结构 #919 Panel: 优化结构,使用 Cell 代替...

王练 ⋅ 04/26 ⋅ 0


原文出处:拓海 大家好!从今天开始,我要与大家一起打造一个属于我们自己的分布式爬虫平台,同时也会对涉及到的技术进行详细介绍。大家如果有什么好的想法请多留言,多提意见,一起来完善我...

拓海 ⋅ 05/06 ⋅ 0





从 Confluence 5.3 及其早期版本中恢复空间

如果你需要从 Confluence 5.3 及其早期版本中的导出文件恢复到晚于 Confluence 5.3 的 Confluence 中的话。你可以使用临时的 Confluence 空间安装,然后将这个 Confluence 安装实例升级到你现...

honeymose ⋅ 今天 ⋅ 0


最近两年,著名的自媒体网站今日头条可以说是火得一塌糊涂,虽然从目前来看也遇到了一点瓶颈,毕竟发展到了一定的规模,继续增长就更加难了,但如今的今日头条规模和流量已经非常大了。 我们...

原创小博客 ⋅ 今天 ⋅ 0


本文讲解 MyBatis 四大核心概念(SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession、Mapper)。 MyBatis 作为互联网数据库映射工具界的“上古神器”,训有四大“神兽”,谓之:Sql...

waylau ⋅ 今天 ⋅ 0


web3j(org.web3j)是Java版本的以太坊JSON RPC接口协议封装实现,如果需要将你的Java应用或安卓应用接入以太坊,或者希望用java开发一个钱包应用,那么用web3j就对了。 web3j的功能相当完整...

汇智网教程 ⋅ 今天 ⋅ 0


重点提示: 线程的本质上只是一个壳子,真正的逻辑其实在“竞态条件”中。 举个例子,比如本题中的打印,那么在竞态条件中,我只需要一个方法即可; 假如我的需求是2个线程,一个+1,一个-1,...

Germmy ⋅ 今天 ⋅ 0

Springboot2 之 Spring Data Redis 实现消息队列——发布/订阅模式

一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式,这里利用redis消息“发布/订阅”来简单实现订阅者模式。 实现之前先过过 redis 发布订阅的一些基础概念和操...

Simonton ⋅ 今天 ⋅ 0

error:Could not find gradle

一.更新Android Studio后打开Project,报如下错误: Error: Could not find com.android.tools.build:gradle:2.2.1. Searched in the following locations: file:/D:/software/android/andro......

Yao--靠自己 ⋅ 昨天 ⋅ 0

Spring boot 项目打包及引入本地jar包

Spring Boot 项目打包以及引入本地Jar包 [TOC] 上篇文章提到 Maven 项目添加本地jar包的三种方式 ,本篇文章记录下在实际项目中的应用。 spring boot 打包方式 我们知道,传统应用可以将程序...

Os_yxguang ⋅ 昨天 ⋅ 0


本文介绍数据结构中几种常见的树:二分查找树,2-3树,红黑树,B树 写在前面 本文所有图片均截图自coursera上普林斯顿的课程《Algorithms, Part I》中的Slides 相关命题的证明可参考《算法(第...

浮躁的码农 ⋅ 昨天 ⋅ 0

android -------- 混淆打包报错 (warning - InnerClass ...)

最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations are missing corresponding EnclosingMember annotation......

切切歆语 ⋅ 昨天 ⋅ 0