How to login to Facebook using CasperJS
After my post about "How to login to Amazon using CasperJS" I received a several emails asking"How to login to Facebook using CasperJS", and I decided to help.
Here you can find more posts about "Why CasperJS is better than PhantomJS", "How to login Amazon using CasperJS" and "How to login Twitter and extract tweets using CasperJS and PhantomJS"
Task: Login to Facebook and get URL of images greater than 100 x 100 pixels.
Here is example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
var casper = require('casper').create({ pageSettings: { loadImages: false,//The script is much faster when this field is set to false loadPlugins: false, userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36' } }); //First step is to open Facebook casper.start().thenOpen("https://facebook.com", function() { console.log("Facebook website opened"); }); //Now we have to populate username and password, and submit the form casper.then(function(){ console.log("Login using username and password"); this.evaluate(function(){ document.getElementById("email").value="YOUR FACEBOOK USERNAME"; document.getElementById("pass").value="YOUR FACEBOOK PASSWORD"; document.getElementById("loginbutton").children[0].click(); }); }); //Wait to be redirected to the Home page, and then make a screenshot casper.then(function(){ console.log("Make a screenshot and save it as AfterLogin.png"); this.wait(6000);//Wait a bit so page loads (there are a lot of ajax calls and that is why we are waiting 6 seconds) this.capture('AfterLogin.png'); }); //Get all images greater than 100x100 pixels casper.then(function(){ var images = this.evaluate(function(){ var facebookImages = document.getElementsByTagName('img'); var allSrc = []; for(var i = 0; i < facebookImages.length; i++) { if(facebookImages[i].height >= 100 && facebookImages[i].width >= 100) allSrc.push(facebookImages[i].src); } return JSON.stringify(allSrc); }); console.log(images); }) casper.run(); |
If you need any help, if you have suggestions please contact me at code.epicenter at gmail.com. Please leave comments if you find this post useful.
http://code-epicenter.com/how-to-login-to-facebook-using-casperjs/How to login to Facebook using CasperJShttp://code-epicenter.com/wp-content/uploads/2015/09/facebook.pnghttp://code-epicenter.com/wp-content/uploads/2015/09/facebook-150x150.pngJavaScriptLibrariesProgrammingTutorialsCasperJS,facebook,PhantomJS,screen scrappingAfter my post about "How to login to Amazon using CasperJS" I received a several emails asking"How to login to Facebook using CasperJS", and I decided to help. Here you can find more posts about "Why CasperJS is better than PhantomJS", "How to login Amazon using CasperJS" and "How to login Twitter and...Amir DuranAmir Duranamir.duran@gmail.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter
Awesome mate !
I really need to try this one, thanks a lot.
Do you think it’s possible to login on every website ? For example I’d like to login google webmaster tool and do several action like crawl my websites, or other stuff webmaster tools API cannot do yet.
Thanks again 🙂
Thx for comment. Yes, it is possible to login on every website, because phantomJs is a headles browser.
Thanks, i’m gonna try this as soon as tomorrow 🙂
I just tried using casper JS to log into google webmaster tools. Login is OK but when I use this.debugPage() I can see
google webmaster tool requires javascript enabled. I don’t understand,so casper JS isn’t able to read and execute javascript ? Thanks for your help if you can :).
I get the message that cookies aren’t enabled.
Hi Francis,
please check your email.
Removed the pageSettings object and it’s all good! 🙂
I’ve got the same message, how did you solved it?
Same, please let me know if you have a working solution.
You are amazing, thanks a lot. I will send you an e-mail shortly.
Hi there. This was quite helpful, but i think there is a mistake.
Once you call “this.wait(6000)” and then “this.capture(…)”, the image gets captured immediately without waiting the 6 seconds.
The idea of “this.wait” is that it receives 2 parameters, first the delay time and then the callback which will be called after the time passed in the first parameter.
Just in case I wasn’t clear enough (I probably wasn’t, sorry for my bad English lol), I leave here how to solve that:
this.wait(6000, function(){
// After 6 seconds, this callback will be called, and then we will capture:
this.capture('AfterLogin.png');
});
Hey,
I’m trying to use this script (with some modifications) to log in Instagram but it just doesn’t work! Could you help me, please!?
The logic should be the same. You have to analyze Instagram’s login page and then customize the script.
Hi, I receive a message: SSL handshake failed. 🙁
Thank you for your comment.