Search code examples
javascriptcasperjs

Casperjs Google Login Not working


I having been working on some code to access my google finance portfolio, but the problem is I need to sign in with my google account. So i have made this:

var casper = require('casper').create();
casper.start('https://accounts.google.com/Login?hl=EN', function() {
  this.evaluate(function(username, password) {
      this.echo(this.getTitle());
      document.querySelector('input#Email').value = username;
      document.querySelector('#next').click();
      document.querySelector('input#Passwd').value = password;
      document.querySelector('#signIn').click();
  }, 'GOOGLE EMAIL', 'PASSWORD');
});

casper.then(function() {
    this.echo(this.getHTML()); // => 'The text included in the <h1 id=foobar>'

    casper.thenOpen('https://www.google.com/finance/portfolio?action=view&pid=1&ei=pBrbVoDhM4iFjAGB-bKIAg', function() {
        this.echo(this.getHTML());
        this.echo(this.getTitle());
    });

});

casper.run();

Which does not log me in!


Solution

  • In my original code I was selecting the wrong input boxes from the google page, instead it should look like this:

    var casper = require('casper').create();
    casper.start("https://accounts.google.com/Login?hl=EN", function() {
      console.log("page loaded...");
      //console.log(this.getHTML());
      //document.querySelector('#Email').value = "kpfromer@gmail.com";
      //document.querySelector('#next').click();
    
      this.fillSelectors('form#gaia_loginform', {
        'input[name="Email"]': 'EMAIL',
      }); //Fills the email box with email
      this.click("#next"); //Fills the email box with email
    
    
      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"]': 'PASSWORD',
            }); //Fill password box with PASSWORD
            this.click("#signIn"); //Click sign in button
            this.wait(500, function() {}); //Wait for it fully sigin
          },
          function fail() {
            console.log("FAIL...");
          }
    
        );
    
      });
    });
    casper.run();
    

    The reason why there are waits is that it takes a little bit for the page to fully load, and swap to other pages.