1

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.

Community
  • 1
  • 1
Q.H.
  • 1,406
  • 2
  • 17
  • 33

1 Answers1

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.

fedevegili
  • 341
  • 3
  • 5
  • Okay, before I had "querySelectAll" outside the evaluate function but this is incorrect because then it wouldn't execute within the page document object. Now it does, thank you. – Q.H. Dec 22 '16 at 20:13