【发布时间】:2013-12-09 21:12:02
【问题描述】:
我对 casperjs 和 JavaScript 知之甚少,一直在尝试测试我们的新网站。在浏览了 casperjs 文档并编写了一些示例程序后,我对 evaluate() 函数有了一些了解。但现在我陷入了一个奇怪的问题。我使用评估功能来查找网页内的所有链接。现在我正在尝试访问所有这些链接并从中获取一些信息。现在我的问题是我第二次使用 evaluate() 函数时它只是被跳过了。这是完全出乎意料的行为。请填写我缺少的内容。我附上了我用来重现问题的示例代码。
var BASE_URL = "http://www.google.com";
var links = [];
var divs = [];
var casper = require('casper').create({verbose: true,});
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
function getDivs(){
__util__.echo("get Divs Function");
var divs = document.getElementsByTagName('div');
return Array.prototype.map.call(divs, function(e) {
return e;
});
}
casper.start(BASE_URL, function() {
this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});
casper.then(function() {
links = this.evaluate(getLinks);
this.echo("links == " + links);
});
casper.waitForUrl(BASE_URL, function(){
this.echo(this.getCurrentUrl());
}, function(){}, 20000);
casper.then(function() {
this.echo("------------------")
divs = this.evaluate(getDivs);
this.echo("^^^^^^^^^^^^^^^^^^")
this.echo("divs == " + divs);
});
casper.run();
【问题讨论】:
标签: javascript casperjs