Intro

PhantomJS is a handy tool for automatizing tasks or testing some specific parts of the system, but sometimes tasks you need to perform are behind login screen which makes your job much harder. I decided to write a running script for PhantomJS which will login to the Amazon using provided username and password. After receiving several emails I implemented a PhantomJS script which logs in into Twitter account and gets all tweets from the stram, and you can check it in my post Login to the Twitter and extract tweets using PhantomJS If you are working with CasperJS then read my post about "How to login to Amazon using CasperJS", How to login Facebook using CasperJS

Basic steps

Every time when you need to handle login system with PhantomJS or CasperJS you have to think about the following things:

  • Submit login form
  • Store cookies received by server
  • Resend cookies upon every new request
  • Save cookies (optional)

Submitting login form defers from system to system, from website to website. One way to submit a form is to send POST request to the login URL. Sometimes, this approach is not good and it will not work, because login form may contain hidden fields/values which are generated dynamically, and system will refuse to login you if you are not submitting these hidden fields.

Better approach is to simulate click on submit button, and this approach is used in my working example.

PhantomJS and CasperJS are automatically managing cookies received from server, which is really helpful, but this feature works only during script execution and once the script is finished, cookies are deleted.

You have to implement file cookie management yourself If you want to store cookies in a file, and read them next time the script is executed.

I had a lot of problems to successfully login on Amazon website, and that is why I decided to share a code with you. Here are the steps that we need to take in order to login Amazon:

  1. Visit Amazon's home page
  2. Simulate click on Sing in button (do not copy and paste URL of sing in page, this link can expire and your PhantomJS script will not work)
  3. On the sign in page, populate username and password fields
  4. Click on Sing in button

Amazon will refuse to login a user if cookes and javascript are not enabled. You can check this if you disable cookies or javascript in your browser (Chrome, Mozilla) and then try to login.

Final solution

Here is the final solution which will login you to the Amazon website. The same pattern can be used for Facebook, Google etc.

If you want to edit the steps, just change the entries in the steps array.

NOTE: Change AMAZON USERNAME and AMAZON PASSWORD with your username and password.

How to run a script

In order to run code above, make the following steps:

  1. Download and save phantomjs.exe on desktop
  2. Make new empty JavaScript file, and save it to the desktop
  3. Copy code above, and paste it to the newly created file
  4. Run CMD, navigate to Desktop and run the following command: phantomjs.exe filename.js

Additional notes

The script is based on the CSS selectors, and may not work in the future if Amazon website heavily changes. If you encounter problems, you can email an author, or you can open Amazon website and check weather IDs of HTML elements (input fields, ID of the login form) are changed.

How to login Amazon using PhantomJS - Working examplehttp://code-epicenter.com/wp-content/uploads/2015/08/phantom.jpghttp://code-epicenter.com/wp-content/uploads/2015/08/phantom-150x150.jpgAmir DuranJavaScriptLibrariesProgrammingTutorialsAmazon,JavaScript,PhantomJS,Web scrapping
Intro PhantomJS is a handy tool for automatizing tasks or testing some specific parts of the system, but sometimes tasks you need to perform are behind login screen which makes your job much harder. I decided to write a running script for PhantomJS which will login to the Amazon using...