phantomjs+casperJS实现模拟登录login.live.com

时间:14-10-15 栏目:网站开发 作者:admin 评论:0 点击: 11,686 次

支持显示使用时间:

casperjs.bat code:

@ECHO OFF
set CASPER_PATH=%~dp0..
set CASPER_BIN=%CASPER_PATH%\bin\
set ARGV=%*
call D:\tools\phantomjs-1.9.7-windows\phantomjs --ignore-ssl-errors=true --output-encoding=GBK "%CASPER_BIN%bootstrap.js" --casper-path="%CASPER_PATH%" --cli %ARGV%

D:\tools\n1k0-casperjs-cd1fab5\batchbin\casperjs.bat jt.js [email protected] password

jt.js code:

var system = require('system');
var fs = require('fs');
var address;

var startTime = new Date();
var liveUrl = 'https://login.live.com/login.srf';

function GetTime(dateM,datetype){  
     var s;                            // 声明变量。  
     var MinMilli = 1000 * 60;         // 初始化变量。  
     var HrMilli = MinMilli * 60;  
     var DyMilli = HrMilli * 24;  
     s="";  
     if (datetype=="d"){  
    s +=Math.round(Math.abs(dateM/DyMilli))+"天";  
     }else if (datetype=="h"){  
    s +=Math.round(Math.abs(dateM/HrMilli))+"小时"  
     }else if (datetype=="m"){  
    s +=Math.round(Math.abs(dateM/MinMilli))+"分";  
     } else{  
    s +=Math.round(Math.abs(dateM/1000))+"秒"  
     }  
     return(s);                        // 返回结果。  
}  
function GetUseTime(){
	return GetTime((Date.parse(new Date())-Date.parse(startTime)),"s");
}
//结束后运行函数
function refresh()  
{ 	 

	//this.echo('refresh');
	phantom.exit();

}  

var live_userid = system.args[4]
var live_passwd = system.args[5]
var RequestVerificationToken = "";

//live_userid = '[email protected]';
//live_passwd = 'wps2006!!';

//require('utils').dump(system.args);

var casper = require('casper').create({
  clientScripts:['includes/jquery.min.js' ],	
  verbose: true,
  logLevel: 'warning',
  pageSettings: { javascriptEnabled: true },
  viewportSize: {width: 1024, height: 768}
 });
casper.options.waitTimeout = 10;

casper.onError = function(msg){
	this.echo("onError:"+msg);
};
casper.start(liveUrl, function(response) {
	// require('utils').dump(response);
	 this.echo(GetUseTime())
	 this.echo('Title:'+this.getTitle()+' start:'+live_userid);
	 this.fill('[name="f1"]', {
		'login': live_userid,
		'passwd': live_passwd
	}, false);

   //this.captureSelector('img/'+live_userid+'_live1.png', 'html');    
   //this.captureSelector('img/'+live_userid+'_live2.png', 'form[name="f1"]');                  

});

casper.then(function() {
  this.click('input[id="idSIButton9"]');
  //this.echo('login...');
});

casper.then(function() {
	this.echo(GetUseTime())
	this.wait(2000,function() {
		this.echo(GetUseTime())

		this.echo('Title:'+this.getTitle());  
		this.capture('img/'+live_userid+'_live3.png');

		if(this.getHTML('div[id="iSummaryPageHeading"]')){//判断登录成功
			this.echo("Name:"+this.getHTML('div[id="idFullNameValue"]').replace(/ /g,''));
			this.echo(live_userid+" login success!");   

			var cookies=[];
			for(var i in phantom.cookies){
				cookies[cookies.length]=phantom.cookies[i]['name']+'='+phantom.cookies[i]['value'];
			}

			 file = fs.open("cookie/"+live_userid+".txt", 'a');//记录cookie
			 file.write(cookies.join('&'));
			 file.close();

		}else{
			this.echo(live_userid+" login fail!");   
		}

	});
});

casper.then(function() {
	this.echo('End useTime:'+GetUseTime());
});
casper.run(refresh);

登录成功后cookie将按用户名保存在 cookie目录下,
截图保存在log目录下

也可以使用:SlimerJS来实现

声明: 本文由( admin )原创编译,转载请保留链接: phantomjs+casperJS实现模拟登录login.live.com

phantomjs+casperJS实现模拟登录login.live.com:等您坐沙发呢!

发表评论


------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。

读者排行