Casperjs Google Login Not working

眉间皱痕 提交于 2019-12-12 05:16:27

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/35822029/casperjs-google-login-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!