CasperJS: URL Iteration

I am new to CasperJS, but there is no way to open the URL and execute CasperJS commands for loops? For example, this code does not work as I expected:

casper.then(function() {
    var counter = 2013;
    for (i = counter; i < 2014; i++) {
        var file_name = "./Draws/wimbledon_draw_" + counter + ".json";
        // getting some local json files
        var json = require(file_name);
        var first_round = json["1"];
        for (var key in first_round) {
            var name = first_round[key].player_1.replace(/\s+/g, '-');
            var normal_url = "http://www.atpworldtour.com/Tennis/Players/" + name;
            // the casper command below only executes AFTER the for loop is done
            casper.thenOpen(normal_url, function() {
                this.echo(normal_url);
            });
        }
    }
});

Instead, Casper is called thenOpenfor iteration for each new URL, it is called only after the for loop is executed. Casper thenOpenthen receives a call with the last value of normal_url. Is there a Casper command to work on every iteration in a for loop?

Procedure: How to make casper thenOpen return the value of the current iteration of the for loop?

Say, for example, I needed a return value for this thenOpen(maybe if the HTTP status is 404, I need to evaluate a different URL, so I want to return false). Can this be done?

casper.thenOpen :

    var status;
    // thenOpen() only executes after the console.log statement directly below
    casper.thenOpen(normal_url, function() {
        status = this.status(false)['currentHTTPStatus'];
        if (status == 200) {
            return true;
        } else {
            return false;
        }
    });
    console.log(status); // This prints UNDEFINED the same number of times as iterations.
+4
2

Fanch Darren Cook, IIFE, url thenOpen.

getCurrentUrl URL-.

this.echo(normal_url);

this.echo(this.getCurrentUrl());

, normal_url , , , . casper.thenOpen(normal_url, function(){...});, . URL-, URL- .


:

then* wait* API- casperjs . , , ( casper.run()). . thenOpen. . thenOpen.

var somethingDone = false;
var status;
casper.thenOpen(normal_url, function() {
    status = this.status(false)['currentHTTPStatus'];
    if (status != 200) {
        this.thenOpen(alternativeURL, function(){
            // do something
            somethingDone = true;
        });
    }
});
casper.then(function(){
    console.log("status: " + status);
    if (somethingDone) {
        // something has been done
        somethingDone = false;
    }
});

this.thenOpen casper.thenOpen somethingDone true casper.then, .


, :

  • i: , , "./Draws/wimbledon_draw_" + i + ".json" not "./Draws/wimbledon_draw_" + counter + ".json"
  • require JSON. , JSON. fs.read, JSON (JSON.parse).

...

. (then* wait*) thenOpen.

+1

, : https://groups.google.com/forum/#!topic/casperjs/n_zXlxiPMtk

IIFE (---).

:

for(var i in links) {
  var link = links[i];

  (function(index) {
    var link = links[index]
    var filename = link.replace(/#/, '');
    filename = filename.replace(/\//g, '-') + '.png';

    casper.echo('Attempting to capture: '+link);
    casper.thenOpen(vars.domain + link).waitForSelector('.title h1', function () {
      this.capture(filename);
    });
  })(i);
}

links , , ...

var links = [{'page':'some-page.html', 'filename':'page-page.png'}, {...}]
+2

Source: https://habr.com/ru/post/1545635/


All Articles