Skip to content

Instantly share code, notes, and snippets.

@ecin
Created April 23, 2012 21:06
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ecin/2473860 to your computer and use it in GitHub Desktop.
Save ecin/2473860 to your computer and use it in GitHub Desktop.
Log into Facebook with phantomJS
console.log("got here");
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.open("http://facebook.com", function(status) {
if ( status === "success" ) {
page.evaluate(function() {
document.querySelector("input[name='email']").value = "email";
document.querySelector("input[name='pass']").value = "pass";
document.querySelector("#login_form").submit();
console.log("Login submitted!");
});
window.setTimeout(function () {
page.render('colorwheel.png');
phantom.exit();
}, 5000);
}
});
@nigina
Copy link

nigina commented Oct 30, 2013

I am continually getting the following error message:

TypeError: 'null' is not an object (evalueting 'document.querySelector("input[name='email']").value = "xxx@gmail.com"')

but email is correct.

@shewu
Copy link

shewu commented Nov 9, 2013

It's not working because http://facebook.com is trying to redirect to https://facebook.com[/index.php], which has the input elements.

@Zahaib
Copy link

Zahaib commented Nov 24, 2013

Nigina, did you manage to make this work?

@amihart
Copy link

amihart commented Jan 4, 2014

I got this to log in to Facebook by changing it up a bit like this:

var page = require('webpage').create();

page.open("http://www.facebook.com/login.php", function(status) {

  if (status === "success") {
    page.evaluate(function() {
        document.getElementById("email").value = "email";
        document.getElementById("pass").value = "password";
        document.getElementById("u_0_1").click();
    });
    window.setTimeout(function() {
       page.render("page.png");
       phantom.exit();
    }, 5000);
  }
});

However, I am unsure of how to actually do anything once it's logged in.
Anyone have any ideas?
I tried adding another "page.evaluate" but it won't evaluate it a second time.

@surenderthakran
Copy link

Is there a way for such redirection to be performed through phantomjs?

@shihshen
Copy link

var page = require('webpage').create();
page.open("http://www.facebook.com/login.php", function(status) {

  if (status === "success") {
    page.onConsoleMessage = function(msg, lineNum, sourceId) {
      console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
    };
    page.evaluate(function() {
      console.log('hello');
      document.getElementById("email").value = "email";
      document.getElementById("pass").value = "password";
      document.getElementById("u_0_1").click();
      // page is redirecting.
    });
    setTimeout(function() {
      page.evaluate(function() {
        console.log('haha');
      });
      page.render("page.png");
      phantom.exit();
    }, 5000);
  }
});

And it seems like I can evaluate twice. Below are the output:
CONSOLE: hello (from line #undefined in "undefined")
CONSOLE: haha (from line #undefined in "undefined")

My phantomjs version is 1.9.7

@manishjo
Copy link

Hi All,

Check my repo for the same.

https://github.com/manishjo/Automation/blob/master/facebook_automation/fbScreenshot.js

Its works fine and login's to facebook and takes a snapshot of your home page.

Thanks,
Manish Joshi

@amirduran
Copy link

Here is a good post explaining how to login to the Facebook and get all images greater than 100x100 pixels from the timeline: http://code-epicenter.com/how-to-login-to-facebook-using-casperjs/

@swapnilkadam92
Copy link

It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working. It's not working.

@steezeburger
Copy link

^ Are you kidding me dude?

@DarckBlezzer
Copy link

DarckBlezzer commented Aug 18, 2016

For all those who can't login, or don't get any of content,
use this command phantomjs --ssl-protocol=any myscript

@grit0
Copy link

grit0 commented Sep 5, 2016

edit "u_0_1" to "loginbutton"

@staplesj
Copy link

staplesj commented Apr 5, 2017

Worked fine for me, but no pictures showed. Is there a way of grabbing the pictures too using PhantomJS?

@patrioticcow
Copy link

document.querySelector("input[name='email']").value = "email";
document.querySelector("input[name='pass']").value = "pass";
document.getElementById("u_0_1").click();

@Shayan20
Copy link

type error araha hai null is not an object

@marcioadr88
Copy link

It gives me this screenshot, any ideas?
colorwheel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment