iro*_*and 8 javascript csv casperjs
我想通过使用Caperjs来下载csv文件.这就是我写的:
var login_id = "my_user_id";
var login_password = "my_password";
var casper = require('casper').create();
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 ');
casper.start("http://eoddata.com/symbols.aspx",function(){
this.evaluate(function(id,password) {
document.getElementById('tl00_cph1_ls1_txtEmail').value = id;
document.getElementById('ctl00_cph1_ls1_txtPassword').value = password;
document.getElementById('ctl00_cph1_ls1_btnLogin').submit();
}, login_id, login_password);
});
casper.then(function(){
this.wait(3000, function() {
this.echo("Wating...");
});
});
casper.then(function(){
this.download("http://eoddata.com/Data/symbollist.aspx?e=NYSE","nyse.csv");
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
我得到了nyse.csv,但该文件是用于注册该网站的HTML文件.
似乎登录过程失败了.如何正确登录并保存csv文件?
在@ Darren的帮助下,我写道:
casper.start("http://eoddata.com/symbols.aspx");
casper.waitForSelector("form input[name = ctl00$cph1$ls1$txtEmail ]", function() {
this.fillSelectors('form', {
'input[name = ctl00$cph1$ls1$txtEmail ]' : login_id,
'input[name = ctl00$cph1$ls1$txtPassword ]' : login_password,
}, true);
});
Run Code Online (Sandbox Code Playgroud)
这段代码最终会出错Wait timeout of 5000ms expired, exiting..据我所知,错误意味着CSS选择器无法找到该元素.我怎样才能找到解决这个问题的方法?
我写的是这样的:
casper.waitForSelector("form input[name = ctl00$cph1$ls1$txtEmail]", function() {
this.fillSelectors('form', {
'input[name = ctl00$cph1$ls1$txtEmail]' : login_id,
'input[name = ctl00$cph1$ls1$txtPassword]' : login_password,
}, true);
}, function() {
fs.write("timeout.html", this.getHTML(), "w");
casper.capture("timeout.png");
});
Run Code Online (Sandbox Code Playgroud)
我timeout.html通过Chrome Developer工具和Firebugs 进行了检查,并且我多次确认有input元素.
<input name="ctl00$cph1$ls1$txtEmail" id="ctl00_cph1_ls1_txtEmail" style="width:140px;" type="text">
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我已经花了几个小时来解决这个问题.
感谢Darren,Urarist和Artjom,我可以删除超时错误,但还有另一个错误.
下载的CSV文件仍然是注册html文件,所以我重写了这样的代码以找出错误原因:
casper.waitForSelector("form input[name ='ctl00$cph1$ls1$txtEmail']", function() {
this.fillSelectors('form', {
"input[name ='ctl00$cph1$ls1$txtEmail']" : login_id,
"input[name ='ctl00$cph1$ls1$txtPassword']" : login_password,
}, true);
});/*, function() {
fs.write("timeout.html", this.getHTML(), "w");
casper.capture("timeout.png");
});*/
casper.then(function(){
fs.write("logined.html", this.getHTML(), "w");
});
Run Code Online (Sandbox Code Playgroud)
在logined.html用户电子邮件中填写正确,但密码未填写.有没有人猜测这个原因?
秘诀就是成功登录。登录方式有多种。我已经尝试过一些方法,在此页面上唯一有效的方法是使用回车键触发表单提交。这是通过使用 PhantomJSpage.sendEvent()函数来完成的。可以使用 来填充这些字段casper.sendKeys()。
var casper = require('casper').create();
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 ');
casper.start("http://eoddata.com/symbols.aspx",function(){
this.sendKeys("#ctl00_cph1_ls1_txtEmail", login_id);
this.sendKeys("#ctl00_cph1_ls1_txtPassword", login_password, {keepFocus: true});
this.page.sendEvent("keypress", this.page.event.key.Enter);
});
casper.waitForUrl(/myaccount/, function(){
this.download("http://eoddata.com/Data/symbollist.aspx?e=NYSE", "nyse.csv");
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
看来需要等待那个特定的页面。CasperJS 没有注意到请求了新页面,并且由于某种原因未使用该then()功能。
我尝试过的其他方法是:
casper.fillSelectors()casper.evaluate()通过单击登录按钮提交casper.click()| 归档时间: |
|
| 查看次数: |
1138 次 |
| 最近记录: |