我一直在研究一些代码来访问我的Google财经投资组合,但问题是我需要使用我的Google帐户登录。所以我已经做到了:
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 :(得分:3)
在我的原始代码中,我从谷歌页面选择了错误的输入框,而不是它应该是这样的:
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();
等待的原因是页面需要一点点才能完全加载,并交换到其他页面。