我是casperJS的新手。我在Windows 8上安装了casperJS 1.0.4和phantomJS 1.8.2。
我的目标是从网上搜集一些数据。我想打开这个webpage并获取佛蒙特州的城镇名单。我复制了Victor W Yee给出的代码。当我运行代码时,它会打开所需的页面,我会将其作为验证的快照,但是当我尝试从表中获取数据时,我会在此行中收到错误:
var town_names_info = this.getElementsInfo(town_selector);
错误说:
TypeError: 'undefined' is not a function(evaluating'this.getElementsInfo(town_selector)')
F:/Trial Codes/intro to casper_JS/Vermont/vermont.js:21
F:/Trial Codes/intro to casper_JS/Vermont:1335 in runStep
F:/Trial Codes/intro to casper_JS/Vermont:332 in checkStep
有什么建议吗?
我的整个代码是:
var utils = require('utils');
var casper = require('casper').create({
verbose: false,
logLevel: 'debug'
});
var url = 'http://en.wikipedia.org/wiki/List_of_towns_in_Vermont';
var town_selector;
casper.start(url, function() {
this.capture("result1.png");
this.echo("* "+this.getTitle()+" *");
});
casper.then(function() {
// Get info on all elements matching this CSS selector
town_selector = 'table[id="sortable wikitable"] tbody tr td:nth-of-type(2)';
var town_names_info = this.getElementsInfo(town_selector); // an array of object literals
// Pull out the town name text and push into the town_names array
var town_names = [];
for (var i = 0; i < town_names_info.length; i++)
{
town_names.push(town_names_info[i].text);
}
// Dump the town_names array to screen
utils.dump(town_names);
});
casper.run(function() {
this.exit();
});
答案 0 :(得分:0)
getElementsInfo()
(请注意页面中的绿色注释)。您可以使用1.1.0-beta3,因为这是&#34; beta&#34;版本实际上是稳定的。当您更新时,您应该使用更新版本的PhantomJS,例如1.9.7或1.9.8(CasperJS存在一些问题)。