CasperJS Evaluate function not returning Array

放肆的年华 提交于 2020-01-05 08:01:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!