This question is similar to others, but the problem I had was more basic.
This is my code:
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return '' + e.getAttribute('href');
});
}
casper.start('');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
});
casper.each(links, function (self, link) {
self.thenOpen(fullURL, function () {
this.echo(this.getTitle() + " - " + link);
});
});
casper.run();
I know that links
get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.
What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:
~ $ casperjs casper-google-disco.js
This question is similar to others, but the problem I had was more basic.
This is my code:
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return 'https://en.wikipedia' + e.getAttribute('href');
});
}
casper.start('https://en.wikipedia/wiki/David_Bowie_discography');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
});
casper.each(links, function (self, link) {
self.thenOpen(fullURL, function () {
this.echo(this.getTitle() + " - " + link);
});
});
casper.run();
I know that links
get created as it is copied from the Quickstart, but I then modified it to open all the links that were found.
What I'm getting is that nothing is echo'd instead of outputting the each title which is what I expect. This is how I'm calling the file:
~ $ casperjs casper-google-disco.js
Share
Improve this question
edited May 23, 2017 at 12:17
CommunityBot
11 silver badge
asked May 16, 2016 at 9:14
icc97icc97
12.9k9 gold badges83 silver badges97 bronze badges
1 Answer
Reset to default 8The fix was very easy in the end, but took me ages to find it as there were no errors and no-one else seemed to have hit this.
The problem is that the links
variable doesn't get set before the each
is called. Putting the each
inside the then
function solves my problem.
The each.js
example in the CasperJS samples was helpful for confirming that you can loop through an array without any need for IIFE.
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('div#mw-content-text table.wikitable tbody tr td i b a');
return Array.prototype.map.call(links, function(e) {
return 'https://en.wikipedia' + e.getAttribute('href');
});
}
casper.start('https://en.wikipedia/wiki/David_Bowie_discography');
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
casper.each(links, function (self, link) {
self.thenOpen(link, function () {
this.echo(this.getTitle() + " - " + link);
});
});
});
casper.run();