CasperJS hangs out when accessing Google Keyword P

2019-04-16 09:33发布

问题:

My goal is to grab keywords from Google Keyword Planner, as the API does not support getting search volume in last 24 months but 12 months only.

I use maily SimpleBrowser, so I am new with CasperJS, I googled some scripts, read documentation and then combined following script.

I can login to Google, even to Adwords dashboard, but when I try to access KeywordPlanner CasperJS freezes, any idea?

JS script

/**
 * Basic vars
 * @type Module utils|Module utils
 */
var utils = require('utils');
var casper = require('casper').create({
    verbose: true,
    logLevel: "debug",
    waitTimeout: 20000
});

/**
 * Start casper and, set the biggest viewport and browser signature
 */
casper.start();
casper.viewport(1280, 1024);
casper.userAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36');

/**
 * Override user agent when requesting source
 * 
 * @param {type} param1
 * @param {type} param2
 */
casper.on('resource.requested', function(resource) {

    for (var obj in resource.headers) {
        var name = resource.headers[obj].name;
        var value = resource.headers[obj].value;
        if (name == "User-Agent"){
            this.echo(value);
        }
    }

});

/**
 * Prevent locking navigation
 */
casper.on('navigation.requested', function (url, navigationType, navigationLocked, isMainFrame) {
    if (url.indexOf('adwords.google.com/ko/KeywordPlanner/Home?__') != -1 || url.indexOf('facebook.com') != -1) {
      this.page.navigationLocked = true;
    }else{
      this.page.navigationLocked = false;
    }
});

/**
 * Main script
 * 
 * @param {type} param
 */
casper.open("https://accounts.google.com/ServiceLogin?service=adwords&continue=https://adwords.google.com/um/identity?#identifier").then(function () {

    console.log("page loaded...");

    this.fillSelectors('form#gaia_loginform', {
        'input[name="Email"]': 'myemail@gmail.com',
    }); //Fills the email box with email
    console.log("email filled...");

    this.click("#next"); //Fills the email box with email
    console.log("next pressed...");


    this.wait(500, function () { //Wait for next page to load
        console.log("Inside WAIT...");

        this.waitForSelector("#Passwd", //Wait for password box

                function success() {
                    console.log("SUCCESS...");
                    this.fillSelectors('form#gaia_loginform', {
                        'input[name="Passwd"]': 'myPassword',
                    }); //Fill password box with PASSWORD
                    this.click("#signIn"); //Click sign in button
                    console.log("Clicked to sign in...");

                    this.wait(5000, function () {

                        console.log("Logged in...");
                        this.capture('1.png');

                        /**
                         * Open the keyword planner
                         */
                        casper.open('https://adwords.google.com/ko/KeywordPlanner/Home?').then(function() {

                             this.wait(10000, function() {
                                console.log("KP opened...");
                                this.capture('2.png');
                            });

                        });

                    });


                },
                function fail() {
                    console.log("FAIL...");
                    this.capture('exit.png');
                }

        );

    });
});

casper.run();

Last console output when Casper freezes

[debug] [phantom] url changed to 

"https://adwords.google.com/ko/KeywordPlanner/H
ome?"
[debug] [phantom] Navigation requested: url=https://adwords.google.com/um/identi
ty?dst=/ko/KeywordPlanner/Home?, type=Other, willNavigate=true, isMainFrame=true

Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] Navigation requested: url=https://adwords.google.com/ko/Keywor
dPlanner/Home?__u=6961347906&__c=8635630266&authuser=0, type=Other, willNavigate
=true, isMainFrame=true
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] url changed to "https://adwords.google.com/ko/KeywordPlanner/H
ome?__u=6961347906&__c=8635630266&authuser=0"
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigat
e=false, isMainFrame=false
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36