2

I'm trying to write a phantomjs script to log in to my facebook account and take a screenshot.

Here's my code:

var page = require('webpage').create();
var system = require('system');
var stepIndex = 0;
var loadInProgress = false;

email = system.args[1];
password = system.args[2];

page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

var steps = [
  function() {
    page.open("http://www.facebook.com/login.php", function(status) {
      page.evaluate(function(email, password) {
            document.querySelector("input[name='email']").value = email;
            document.querySelector("input[name='pass']").value = password;

            document.querySelector("#login_form").submit();

            console.log("Login submitted!");
      }, email, password);
      page.render('output.png');
    });
  },
  function() {
    console.log(document.documentElement.innerHTML);
  },
  function() {
    phantom.exit();
  }
]

setInterval(function() {
  if (!loadInProgress && typeof steps[stepIndex] == "function") {
    console.log("step " + (stepIndex + 1));
    steps[stepIndex]();
    stepIndex++;
  }
  if (typeof steps[stepIndex] != "function") {
    console.log("test complete!");
    phantom.exit();
  }
}, 10000);

(Inspired by this answer, but note that I've upped the interval to 10s)

Called like so:

./phantomjs test.js <email> <password>

With output (filtering out the selfxss warnings from Facebook):

step 1
load started
load finished
Login submitted!
load started
load finished
step 2
<head></head><body></body>
step 3
test
complete!

(Note that the html output in step two is empty)

This answer suggests that there are problems with phantomjs' SSL options, but running with --ssl-protocol=any has no effect.

This appears to be a similar problem, but for caspar, not phantomjs (and on Windows, not Mac) - I've tried using --ignore-ssl-errors=yes, but that also had no effect.

I guessed that this might be a redirection problem (and, indeed, when I replicate this on Chrome, the response from clicking "Submit" was a 302 Found with location https://www.facebook.com/checkpoint/?next), but according to this documentation I can set a page.onNavigationRequested handler - when I do so in my script, it doesn't get called.

I think this issue is related, but it looks as if there's no fix there.

Community
  • 1
  • 1
scubbo
  • 4,969
  • 7
  • 40
  • 71
  • 1
    You can't tell whether it works or not with this script. Wrap `document.documentElement.innerHTML` in `page.evaluate(function(){return document.documentElement.innerHTML;})`. – Artjom B. Apr 30 '15 at 21:30
  • That seems to work. Thanks! Can you explain why page.evaluate(...) is necessary to enact the redirect? I would have expected it to be carried out automatically. – scubbo Apr 30 '15 at 22:27
  • The redirect works fine. It's just that `document` has no meaning outside of the page context. – Artjom B. Apr 30 '15 at 22:31

0 Answers0