【发布时间】:2017-05-25 09:24:07
【问题描述】:
我正在尝试使用 casperjs 进行一些测试,这里的特定情况是:
从下拉菜单中提取城市名称,(已完成)
然后选择每个城市(带有
casper.fill()),这会导致加载新 页面上的内容和 URL 发生变化,(成功在使用单个城市名称进行测试时,失败在城市名称列表中循环)通过新加载项的链接(新页面)更进一步,
最后,抓取每一页的内容
我试图做一个循环来遍历城市列表并在每个循环中完成所有工作。但问题是CasperJs 试图立即将<option> 字段值一个接一个地设置为每个城市,而不执行循环内的其余代码:
casper.then(function() {
var citiesLength = cities.length;
for (var i = 0; i < citiesLength; i++) {
this.fill('form.wpv-filter-form',{ //setting drop-down field value to the city names in order of the items in the array
'city[]': cityNames[i]
});
// Apparently the code below (to the end of the loop) doesn't get executed
casper.thenEvaluate(function() {
// Here the url change is being checked to know when the new content is loaded:
var regexString = '(\\?)(city)(\\[\\])(=)(' + cityNames[i] + ')&';
var regex = new RegExp(regexString, "igm");
this.waitForUrl(regex, function(){
var name = this.getHTML('.kw-details-title');
link = this.evaluate(getFirstItemLink); // for test, just getting the first item's link
casper.open(link).then(function(){
this.echo("New Page is loaded......");
// Grab the single item contents
});
});
});
}
这是日志(缩短了 3 个城市):
[debug] [remote] Set "city[]" field value to city1
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[debug] [remote] Set "city[]" field value to city2
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[debug] [remote] Set "city[]" field value to city3
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [phantom] Step anonymous 5/5: done in 123069ms.
[info] [phantom] Step _step 6/79 https ://domain.com/section/ (HTTP 200)
[info] [phantom] Step _step 6/79: done in 123078ms.
P.s: 使用casper.open() 是否是到达二级页面(项目页面)的好方法?获取内容后是否需要以某种方式关闭它们?
谢谢
【问题讨论】:
标签: javascript web-scraping casperjs