问题
Note, I've already looked at:
Understanding the evaluate function in CasperJS
I am trying to write a simple web-scraper to download all pdf's on my professor's webpage.
Here is my code:
var casper = require('casper').create({verbose: true , logLevel: "debug" });
var url = "https://www.cs.rit.edu/~ib/Classes/CSCI264_Fall16-17/assignments.html";
casper.start(url);
var elements;
try {
casper.then(function(){
try {
// statements
elements = this.evaluate(function(){ return __utils__.findAll('body ul li a'); });
console.log("elements: " + elements);
console.log(this.getCurrentUrl());
} catch(e) {
// statements
console.log(e);
}
});
} catch(e) {
console.log(e);
}
casper.run();
The elements
array size given back is always zero but when I put
__utils__.echo(__utils__.findAll('body ul li a').length);
I get the correct amount of links.
Is this because the evaluate function won't return an array of elements?
Any help would be appreciated.
回答1:
Just use native js methods instead of __utils__
provided by casperjs, example:
elements = this.evaluate(function(){ return document.querySelectorAll('body ul li a'); });
I'm not sure why findAll
didn't work.
来源:https://stackoverflow.com/questions/41290403/casperjs-evaluate-function-not-returning-array