我想使用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();
我知道了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);
});
并且此代码最终以error结束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");
});
我timeout.html
通过Chrome Developer工具和Firebug进行了检查,并多次确认存在该input
元素。
<input name="ctl00$cph1$ls1$txtEmail" id="ctl00_cph1_ls1_txtEmail" style="width:140px;" type="text">
我该如何解决这个问题?我已经花了几个小时解决这个问题。
感谢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");
});
在logined.html
用户中,电子邮件已正确填写,但密码未填写。有谁能猜出这个的原因吗?
诀窍是成功登录。有多种登录方式。我已经尝试了一些方法,并且此页面上唯一可行的方法是使用Enter键触发表单提交。这是通过使用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();
似乎有必要等待该特定页面。CasperJS不会注意到请求了新页面,并且由于某种原因未使用该then()
功能。
我尝试过的其他方法是:
casper.fillSelectors()
casper.evaluate()
并通过单击提交按钮提交casper.click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句