安装
- phantomjs安装
进入官网下载压缩包:http://phantomjs.org/download.html
解压到c盘并改名为phantomjs,并把路径(C:\casperjs\bin\casperjs.exe)加入到电脑环境变量里 - casperjs安装
进入官网下载压缩包:http://casperjs.org/
解压到c盘并改名为casperjs,并把路径(C:\phantomjs\bin\phantomjs.exe)加入到电脑环境变量里
执行一个Casperjs文件
cd进入js脚本的路径位置
casperjs test.js
配置参数
var casper = require('casper').create({
//实时输出日志
verbose: true,
//日志打印级别
logLevel: 'error',
//步骤超时
onStepTimeout: function(){},
//超时等待时间
waitTimeout: 120000,
//超时执行
//onTimeout: function(){},
//超时重启
//retryTimeout: 120000,
//步骤执行完毕
onStepComplete:function(){},
//页面访问设置
pageSettings: {
//是否加载图片
'loadImages': false,
//用户代理
'userAgent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
},
// 浏览器窗口大小
viewportSize: {
width: 1024,
height: 768
},
//需要加载的脚本文件
clientScripts:[
'js/jQuery-2.1.4.min.js'
],
//当网页请求返回的状态码与这里配置的一致时调用
httpStatusHandlers:{
400: function(){}
},
//页面初始化完成
onPageInitialized:function(){},
//抛出错误时退出脚本
exitOnError: true,
//禁止报错
silentErrors: false,
//抛出错误时
onError: function () {},
//程序退出时
onDie: function () {},
//页面出现alert弹窗时
onAlert: function () {}
});