Friday 12 August 2016

Textual description of firstImageUrl

Download WebMaster Tool Data using Casper JS

This is a simple casper js script to automate your process of downloading webmaster tools reports .


Google Webmaster does not provide any api to download the data .

This simple casperjs script will allow you to download the data and automate the process .

please change the google account credentials and also the time period for which you want to download the report .
var page = require('webpage').create();
var xpath = require('casper').selectXPath;
var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         // The WebPage instance used by Casper will
         loadPlugins: false,         // use these settings
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'https://accounts.google.com/ServiceLogin';

casper.start(url, function() {
   // search for 'casperjs' from google form
   console.log("page loaded");
   this.fill('form#gaia_loginform', { 
        Email:'test', 
        Passwd:'test'
    }, true);
});

casper.thenEvaluate(function(){
   console.log("Page Title " + document.title);
});
var url = 'https://www.google.com/webmasters/tools/top-search-queries?hl=en&siteUrl=http://www.javaroots.com/&de=20150315&db=20150314&more=true&type=queries';
casper.thenOpen(url ,function (status) {
    /*var js = page.evaluate(function () {
        return document;
    });
    console.log(JSON.stringify(js)); */
 console.log(" trrsadsadsssssss");
});

casper.then(function() {
 console.log(" clickingggg first time ");
 this.click('#tsq-table-download > div:nth-child(1)');
 //this.click(xpath('//*[@class="goog-inline-block jfk-button jfk-button-standard"]'));
});


//casper.waitForSelector(xpath('/html/body/div[11]/div[3]/button[1]'), function() {
 casper.waitForSelector('.goog-buttonset-default', function() {

casper.then(function(){
  this.capture('abc.png', undefined, {
             format: 'png',
            quality: 100
     });
});
});

casper.then(function() {
 console.log(" clickingggg");
 //this.click(xpath('/html/body/div[11]/div[3]'));
 this.click('.goog-buttonset-default');
});

casper.on('resource.received', function(resource) {
 if(resource.url)
 {
  if(resource.url.indexOf("/top-search-queries-dl") > -1)
  {
   console.log(" resource url is " + resource.url);
   console.log("Downloading csv file");
   this.download(resource.url, 'ExportData.csv');
  }
 }
 
});
casper.run();
Post Comments and suggestions !!!!