Skip to content

Instantly share code, notes, and snippets.

@dariusk
Created November 14, 2012 14:39
Show Gist options
  • Save dariusk/4072489 to your computer and use it in GitHub Desktop.
Save dariusk/4072489 to your computer and use it in GitHub Desktop.
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
Copy link

RauenS commented Oct 20, 2013

Hello!I have a problem.Can you help me([email protected])

@awaisakbar2020
Copy link

awaisakbar2020 commented Apr 19, 2020

Hi,
This code is not working its probably because amazon doesn't show both user name and password fields on one page. The first page has a username field while the second is for the password, so there are two forms in total. How to make this code work now? how to login to amazon when we have to submit two forms on two different pages.

@dariusk
Copy link
Author

dariusk commented Apr 27, 2020

Sorry, this was written against a seven-year-old version of the Amazon home page. You'll need to completely rewrite this.

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