querySelectorAll not recognizing var

坚强是说给别人听的谎言 提交于 2019-12-01 14:28:19

CasperJS and PhantomJS have two contexts. The inner context which is programmed through casper.evaluate() is sandboxed. It means that it has no access to variables defined outside. You need to explicitly pass those variables in:

var index = 'div.list > div > a[href*="' + tier + '"]';

function getBattles(innerIndex) {
    var battles = document.querySelectorAll(innerIndex);
    return Array.prototype.map.call(battles, function(e) {
        return e.getAttribute('href');
    });
}
...
battles = casper.evaluate(getBattles, index);

The PhantomJS documentation of evaluate() has an important note:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

Closures, functions, DOM nodes, etc. will not work!

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