嵌套的 getElement 函数在 casperjs 上不起作用

Nested getElement functions doesnt work on casperjs

你好我正在学习casperjs。但是我的代码有问题

document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a').length;

当我 运行 脚本时,它根本不起作用。但是当我在 Mozilla/chrome 控制台中写入时它起作用了。在控制台中,它给我预期的“5”,但在 cmd 上它给了我

TypeError:'undefined is not an object <evaluating 'document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a').length;

我知道 undefined 意味着它无法在页面上找到它,但是当我截图时它显示它在正确的页面上。所以它必须找到?但不幸的是它没有。我的代码在下面。感谢您的帮助:)

phantom.casperPath = 'C:/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');
var casper = require('casper').create({
    pageSettings: {
        loadImages: false,//The script is much faster when this field is set to false
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }
});

casper.start().thenOpen("https://angel.co/mike_greenfield?utm_source=people", function () {
    console.log("site opened");
});

casper.then(function () {
    this.capture('pic.png');
});
casper.then(function () {
var howmanylinks= document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a').length;
    console.log(howmanylinks);
});
casper.then(function () {
    casper.exit();
});
casper.run();

DOM 只能在 casper.evaluate() 内部访问。虽然,document在外面可用,但它没有做任何事情:

casper.then(function () {
    var howmanylinks = this.evaluate(function(){
        return document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a').length;
    });
    console.log(howmanylinks);
});