login to amazon associates with phantomjs

125 views
Skip to first unread message

la...@convexicon.com

unread,
Apr 17, 2017, 1:07:09 PM4/17/17
to phantomjs
I've written a code (based on an example I saw somewhere) that is supposed to login to my Amazon Associates account and capture my balance. However, for some reason it captures the login page instead, with a div at the top that says that cookies must be enabled. This is unclear to me, because according to the script the cookies are enabled. The script is below, any help will be appreciated:

var steps=[];
var testindex = 0;
var loadInProgress = false;//This is set to true when a page is still loading

/*********SETTINGS*********************/
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/*********SETTINGS END*****************/



console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/

var AMAZON_USERNAME = 'xxxxx';
var AMAZON_PASSWORD = 'xxxxx';

steps = [

    function(){
        console.log('Step 1 - Open Amazon home page');
        page.open("https://affiliate-program.amazon.com/", function(status) {
        });
    },
    
    function() {
        console.log('Step 2 - Click on the Sign in button');

        page.evaluate(function(){
            document.getElementById("a-autoid-0-announce").click();
        });
    },
  //Step 3 - Populate and submit the login form
    function() {
        console.log('Step 3 - Populate and submit the login form');
         page.evaluate(function(){
            document.getElementById("ap_email").value = AMAZON_USERNAME;
            document.getElementById("ap_password").value = AMAZON_PASSWORD;
            document.getElementById("signInSubmit").submit();
         });
    },
  //Step 4 - Wait Amazon to login user. After user is successfully logged in, user is redirected to home page. Content of the home page is saved to AmazonLoggedIn.html. You can find this file where phantomjs.exe file is. You can open this file using Chrome to ensure that you are logged in.
    function() {
        console.log("Step 4 - Wait Amazon to login user. After user is successfully logged in, user is redirected to home page. Content of the home page is saved to AmazonLoggedIn.html. You can find this file where phantomjs.exe file is. You can open this file using Chrome to ensure that you are logged in.");
        var fs = require('fs');
        
        var result = page.evaluate(function() {   
            return document.querySelectorAll("html")[0].outerHTML;
        });

        fs.write('AmazonLoggedIn.html',result,'w');
    },
];
/**********END STEPS THAT FANTOM SHOULD DO***********************/

//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50);

function executeRequestsStepByStep(){
    if (loadInProgress == false && typeof steps[testindex] == "function") {
        //console.log("step " + (testindex + 1));
        steps[testindex]();
        testindex++;
    }
    if (typeof steps[testindex] != "function") {
        console.log("test complete!");
        phantom.exit();
    }
}

/**
 * These listeners are very important in order to phantom work properly. Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('Loading started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
    console.log(msg);
};


Reply all
Reply to author
Forward
0 new messages