文档章节

installing phantomjs and casperjs on ubuntu

Airship
 Airship
发布于 2017/07/24 20:15
字数 1024
阅读 9
收藏 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
粉丝 36
博文 865
码字总数 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

没有更多内容

加载失败,请刷新页面

加载更多

下一页

Ubuntu18.04 显卡GF-940MX安装NVIDIA-390.77

解决办法: 下面就给大家一个正确的姿势在Ubuntu上安装Nvidia驱动: (a)首先去N卡官网下载自己显卡对应的驱动:www.geforce.cn/drivers (b)下载后好放在英文路径的目录下,怎么简单怎么来...

AI_SKI
今天
1
0
深夜胡思乱想

魔兽世界 最近魔兽世界出了新版本, 周末两天升到了满级,比之前的版本体验好很多,做任务不用抢怪了,不用组队打怪也是共享拾取的。技能简化了很多,哪个亮按哪个。 运维 服务器 产品 之间的...

Firxiao
今天
1
0
MySQL 8 在 Windows 下安装及使用

MySQL 8 带来了全新的体验,比如支持 NoSQL、JSON 等,拥有比 MySQL 5.7 两倍以上的性能提升。本文讲解如何在 Windows 下安装 MySQL 8,以及基本的 MySQL 用法。 下载 下载地址 https://dev....

waylau
今天
0
0
微信第三方平台 access_token is invalid or not latest

微信第三方开发平台code换session_key说的特别容易,但是我一使用就带来无穷无尽的烦恼,搞了一整天也无济于事. 现在记录一下解决问题的过程,方便后来人参考. 我遇到的这个问题搜索了整个网络也...

自由的开源
今天
3
0
openJDK之sun.misc.Unsafe类CAS底层实现

注:这篇文章参考了https://www.cnblogs.com/snowater/p/8303698.html 1.sun.misc.Unsafe中CAS方法 在sun.misc.Unsafe中CAS方法如下: compareAndSwapObject(java.lang.Object arg0, long a......

汉斯-冯-拉特
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部