brainstormtrooper
brainstormtrooper

Reputation: 495

Get CasperJS to iterate over a list of URLs

I have 3 URLs to loop over looking for a file to download.

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();

but only the last element is echoed. "Starting URL..." only echoes once. Why isn't Casper iterating over the whole array?

Upvotes: 0

Views: 233

Answers (1)

DavidDomain
DavidDomain

Reputation: 15293

Looks like you are restarting casper on each iteration. Instead of using thenOpen after the first iteration.

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:...

Try the following:

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();

Upvotes: 1

Related Questions