之所以用casperjs,是因为用后台代码解决不了登陆支付宝,所以迫于无奈。 所以现在用它爬虫,这是一个无界面的浏览器,介绍不多说,现在我用来模拟登陆支付宝,然后进行支付。
phantom.outputEncoding="GBK";
var casper = require('casper').create();
var url = casper.cli.args[0];
console.log(url);
//打开浏览器
casper.start();
//打开页面
casper.thenOpen(url);
//开始
casper.then(function() {
//点击“账号密码登录”
this.click("h5 a[seed=v5_need_phonelogin-login]");
//截图
this.capture("1.png");
this.wait(3000, function () {
this.echo('jump login');
});
});
//登录成功后,填写表单
casper.then(function() {
this.sendKeys('input[name=logon_id]','18888888888',{reset:true});//支付宝账号
this.sendKeys('input[name=pwd_unencrypt]','123456',{reset:true});//支付宝 支付密码
this.click("button[seed=v5_need_login_new-submit]");
//等待3秒
this.wait(3000, function () {
result = casper.evaluate(function(){
var payText = document.querySelector('button[seed=v5_cashier_pre_confirm-submit]').innerText;
if(payText.indexOf("确认付款") != -1){
//付款
document.querySelector('button[seed=v5_cashier_pre_confirm-submit]').click();
return 'success';
} else{
return 'fail ';
}
});
this.echo(result);
});
})
casper.then(function() {
casper.exit();
});
// 将前面定义的步骤 跑起来
casper.run();