CasperJS on returning links selectively problem

19 views
Skip to first unread message

Rob

unread,
Oct 26, 2016, 3:36:36 AM10/26/16
to CasperJS
The following code works as advertised:

var links = [];

var processPage = function() {
  links = links.concat(this.evaluate(getLinks));
};

function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.href;
});
return links;
}

....
casper.start();

casper.thenOpen(link).waitForText('Some Text', processPage);

casper.run();

But, if I want to be selective about the links accumulated and I adjust the getLinks method as follows:

function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
if (e.text === 'Some Text') {
return e.href;
}
});
return links;
}

Seems a reasonable thing to do, however, the function returns nothing. e.text contains valid data.

I might have the wrong end of the Javascript stick here because I'm relatively new at this stuff. Maybe it has something to do with what happens when the if statement is false. In that case, I don't know how to handle that one.

Would appreciate it if I could be pointed in the right direction.

Ken

unread,
May 25, 2017, 6:59:06 PM5/25/17
to CasperJS
It doesn't look like that is the place to filter by condition. That part is meant to return the href link for each item matched. Putting that code might return garbage for non-matching text I guess and perhaps corrupt the whole result. Maybe you can do the filtering directly on the links variable that is returned. -> links = links.concat(this.evaluate(getLinks));) Iterate through that and process only if that is what you what.
Reply all
Reply to author
Forward
0 new messages