2016-03-06 80 views
0

我一直在努力的一些代码来访问我的谷歌金融投资组合,但问题是我需要登录我的谷歌帐户。所以我做了这个:Casperjs谷歌登录不工作

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(); 

哪些不会登录我!

+0

您是否收到任何类型的错误消息?如果是这样,请告诉我们。 – leDominatre

+0

我没有收到任何错误消息。我的代码没有工作,因为我没有登录进程。 – Kpfromer

+0

沿着屏幕截图查看错误的位置。您使用哪个PhantomJS版本?请注册到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([示例](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file -2_caspererrors-JS))。也许有错误。 –

回答

2

在我的原代码,我是从谷歌的页面中选择了错误的输入框,取而代之的应该是这样的:

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 = "[email protected]"; 
    //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(); 

为什么有等待的是,它需要一点点的页面的原因完全加载并交换到其他页面。

+0

新登录不再有效。 – Murali