因为之前使用casperjs登录过live.com(phantomjs+casperJS实现模拟登录login.live.com),所以使用相同的办法去登录facebook.虽然登录成功了,但截图看到的内容不正确:只能看到导航条,下面部分内容好像没有初始化。
正在一筹莫展时,想到去stackoverflow.com问问看有没有人能帮助我,于是发了一个救助贴:
很快就有人回复了,感谢:Artjom B.
但是按照他提供的方法,等待dom加载和,等待时间的方法都试过没有成功。便再次向他提问,并附近上原始代码和效果截图。
第二天看到他的回答有更新,主要有几点:
1、--ssl-protocol=tlsv1
2、waitForSelector
3、wait
4、 page.error和page.initialized
http://docs.casperjs.org/en/latest/events-filters.html#page-error
http://stackoverflow.com/questions/25359247/casperjs-bind-issue/25359714#25359714
解决问题主要是第4项里的2个关键设置,通过定义page.error发现有输出:
PageError: TypeError: 'undefined' is not an object (evaluating 'bigPipe.onPageletArrive')
错误。
此错误表明初始化缺少的东西,并参考他提供的page.initialized定义,解决了上面这个错误。
文件名是:facebook.js 以下是可以截图正常的代码:
/**
* 登录facebook并截图:使用方法:
* casperjs --ssl-protocol=tlsv1 facebook.js --user=youemail --pass=yourpassword
*/
var system = require('system');
var fs = require('fs');
var address;
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
javascriptEnabled: true ,
loadImages: true, // The WebPage instance used by Casper will
loadPlugins: true, // use these settings
userAgent:' Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'
},
viewportSize: {width: 1024, height: 768}
});
casper.options.waitTimeout = 10000;
casper.on("page.error", function(msg, trace) {
this.echo("PageError: " + msg, "ERROR");
});
casper.on( 'page.initialized', function(){
this.evaluate(function(){
var isFunction = function(o) {
return typeof o == 'function';
};
var bind,
slice = [].slice,
proto = Function.prototype,
featureMap;
featureMap = {
'function-bind': 'bind'
};
function has(feature) {
var prop = featureMap[feature];
return isFunction(proto[prop]);
}
// check for missing features
if (!has('function-bind')) {
// adapted from Mozilla Developer Network example at
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
bind = function bind(obj) {
var args = slice.call(arguments, 1),
self = this,
nop = function() {
},
bound = function() {
return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
};
nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
bound.prototype = new nop();
return bound;
};
proto.bind = bind;
}
});
});*/
//var user_email = system.args[4];
//var user_pass = system.args[5];
var user_email = casper.cli.get("user");
var user_pass = casper.cli.get("pass");
casper.start("http://www.facebook.com/login.php", function(response) {
this.page.evaluate(function(a,b) {
document.querySelector("input[name='email']").value = a
document.querySelector("input[name='pass']").value = b;
document.querySelector("#login_form").submit();
console.log("Login submitted!");
},user_email,user_pass);
this.capture('img/'+user_email+'_login0.png');
}).then(function() {
this.echo('Title:'+this.getTitle());
this.echo(this.getCurrentUrl());
this.capture('img/'+user_email+'_login1.png');
if(this.getPageContent().indexOf('"firstName"')!=-1){
this.echo("login success!");
}else{
this.echo("login fail!");
phantom.exit();
}
}).waitForSelector("[id='u_0_1n']", function(){
this.capture('img/'+user_email+'_login2.png');
}).wait(4000, function(){
this.capture('img/'+user_email+'_login3.png');
}).run();
简单记录下,希望能帮助到碰到类似问题的朋友。
声明: 本文由( admin )原创编译,转载请保留链接: 使用casperjs登录facebook并截图不完整问题和解决
------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。