javascript - 让 CasperJS 迭代 URL 列表

标签 javascript arrays casperjs

我有 3 个 URL 需要循环查找要下载的文件。

lst = ["BX", "BL", "BM"];
lst.forEach(function(i){
    var url = 'http://www.tzg-infocenter.com/visubl_php//images/' + i + '/' + clip + '/' +  casper.cli.raw.get('entryDate') + '/' + ref + '.PDF';
    casper.start(url,function(){

        this.echo('>>> Starting URL ' + url);
    }
});
casper.run();

但只有最后一个元素被回显。 “起始 URL...”仅回显一次。为什么 Casper 不迭代整个数组?

最佳答案

看起来您正在每次迭代中重新启动 casper。而不是在第一次迭代后使用 thenOpen

thenOpen

Signature: thenOpen(String location[, mixed options])

Adds a new navigation step for opening a new location, and optionally add a next step when its loaded:...

尝试以下操作:

var casper = require('casper').create();

var urls = ['https://google.com', 'http://casperjs.org/', 'http://phantomjs.org']

urls.forEach(function(url, idx) {

  if ( idx > 0 ) {
    casper.thenOpen( url, function() {
      this.echo(this.getTitle());
    });
  } else {
    casper.start( url, function() {
      this.echo(this.getTitle());
    });
  }

})


casper.run();

关于javascript - 让 CasperJS 迭代 URL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43911237/

相关文章:

javascript - 函数定义中使用的命名数组元素

arrays - 在 Go 中序列化混合类型的 JSON 数组

javascript - 使用 CasperJS 正确地遍历数组和打开页面

javascript - CasperJS 在 NodeList 中返回 null 元素

node.js - 在幻象中运行 'casperjs test'

javascript - 如何在 node.js 中的 http PUT 正文中使用 JSON?

javascript - 是否可以预取网页音频并尽早播放,使其与视频同步?是否有一个javascript命令?

javascript - 如何同时单击一个按钮并激活2个按钮

javascript - 引用这个来轻松确定范围是好主意还是坏主意?

javascript - 无法将修改后的字符串推送到 javascript 数组