2016-03-06 78 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