PhantomJS Stack Exchange Login
Nathan Osman — 8 years, 8 months ago


var CONFIG = {
email: '###',
password: '###'
};
// Import the webpage module
var webPage = require('webpage');
// The callback will be invoked when the login succeeds.
function login(callback) {
// Load the initial login page
var loginPage = webPage.create();
// Process each step of the login process
loginPage.onLoadFinished = function(status) {
// Check for an error
if(status != 'success') {
console.log("Unable to display login page.");
phantom.exit();
}
// This technique is fragile but it works for now - just keep
// feeding in the login credentials and mashing the submit button
// until the Stack Exchange homepage is loaded
if(loginPage.url.match(/^https?:\/\/stackexchange\.com\/?/)) {
callback();
} else {
loginPage.switchToFrame('affiliate-signin-iframe');
loginPage.evaluate(function(cfg) {
$('#email').val(cfg.email);
$('#password').val(cfg.password);
$('input[type=submit]').click();
}, CONFIG);
}
};
// Load the page
loginPage.open('https://stackexchange.com/users/login#log-in');
}