用casperjs模拟登录,支持多个账户登录

var casper = require('casper').create({
    viewportSize:{
        width:1920,
        height:1080
    }
});

var url1 = 'http://jifen.yuyid.com';

var users = [{'username':'150****656','password':'xxxxxx'},{username:'username',password:'xxxxx'}];
casper.start();

var autoSingin = function(value,key){
    //打开首页
    casper.thenOpen(url1,function(){
        this.captureSelector('jifen.png','html');
        this.wait(1000); //this.wait方法挡不住当前回调函数下面代码的执行,只能延时下一步要执行的代码,比如下面这个casper.then
        this.click('div.go-login');
    })

    //填充表单并登录
    casper.then(function(){
        //等待1s后执行这个回调
        this.captureSelector('alert.jpg','html');
        this.fill("form#login_form",value,false)

        this.captureSelector('filled.jpg','html');

        this.click('a.login-btn');
        this.wait(3000)
    })

    //点击签到打开窗口
    casper.then(function(){
        this.captureSelector('logined.jpg','html');
        this.click('#sign-btn');
        this.wait(1000);
    })

    //执行签到
    casper.then(function(){
        this.captureSelector('click-sigin.png','html');
        this.click('.sign-btn-a');
        this.wait(1000);
    })

    //签到成功
    casper.then(function(){
        this.capture('sigin-success-'+key+'.png');
        this.click('.user>a')
    })

}

users.forEach(function(value,key){
    autoSingin(value,key)
})

casper.run();

 备注:fill方法可以用json键值对的形式填充表单数据,如果是fillSelectors方法,则必须是dom-value的形式,比如:

//'#TANGRAM__PSP_3__userName'为input的id
this.fillSelectors('form#TANGRAM__PSP_3__form',{
'#TANGRAM__PSP_3__userName':'150****6728',
'#TANGRAM__PSP_3__password':'keivn**168'
},false);

 

posted @ 2017-05-09 18:29  向着太阳生  阅读(1067)  评论(0编辑  收藏  举报