casperjs - then() method only opening up the same link -
i have array of links , follow code supposed open each link in array of links.
var x; var = 0; casper.start(url, function() { x = links.split(" "); // x array of links }); casper.then(function() { this.each(x, function() { this.thenopen((partialurl + x[i]), function() { this.echo(this.gettitle()); // display title of page i++; // change link being opened }); }); }); casper.run();
the problem opens first link , keeps opening link on , on again. it's i
isn't changing.
one of casperjs examples had same problem twitter followers, open link each twitter follower, display 1 name (the last 1 in list) entire time.
here example:
var users = ['subelsky','bmorejs']; var casper = require('casper').create(); var idx,data,user; var length = users.length; casper.start(); (idx=0; idx < length; idx++) { user = users[idx]; casper.thenopen('http://mobile.twitter.com/' + user,function() { data = this.evaluate(function(location) { return document.queryselector('div.profile td.stat.stat-last div.statnum').innertext; }); this.echo(user + ": " + data); }); } casper.run();
the output bmorejs: 2861 followers
, bmorejs: 503 followers
can changed?
it seems runs through of links before gets function thenopen
, , instead opens links. it's easy fix.
simply move i++;
statement above thenopen
method, , way sure change before opens new link. also, change initial value of i
-1
when runs i++;
, won't skip 1
right away.
your code change following:
var x; var = -1; casper.start(url, function() { x = links.split(" "); // x array of links }); casper.then(function() { this.each(x, function() { i++; // change link being opened (has here specifically) this.thenopen((partialurl + x[i]), function() { this.echo(this.gettitle()); // title should different }); }); }); casper.run();
Comments
Post a Comment