Instantly share code, notes, and snippets.

Embed
What would you like to do?
Logging in to Amazon using PhantomJS
// phantomjs code to log in to Amazon
// based on the code from this Stackoverflow answer: http://stackoverflow.com/questions/9246438/how-to-submit-a-form-using-phantomjs
// I'm injecting jQuery so this assumes you have jquery in your project directory
var page = new WebPage(), testindex = 0, loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
var steps = [
function() {
console.log("Load Login Page");
page.settings.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.6 Safari/537.11";
page.open("https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin");
},
function() {
console.log("Enter Credentials");
page.injectJs("jquery.min.js");
page.evaluate(function() {
$('#ap_email').val('AMAZON EMAIL ACCOUNT');
$('#ap_password').val('AMAZON PASSWORD');
console.log(document.title);
});
},
function() {
console.log('login');
page.evaluate(function() {
console.log(document.title);
$('#ap_signin_form').submit();
});
}
]
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
//page.render("images/step" + (testindex + 1) + ".png");
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
@RauenS

This comment has been minimized.

RauenS commented Oct 20, 2013

Hello!I have a problem.Can you help me(rasagirov@gmail.com)

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