为什么我不能在Casperjs中使用indexOf?

时间:2013-12-13 21:48:56

标签: javascript casperjs

我有以下代码(尝试使用casperjs运行它):

var casper = require('casper').create({
    viewportSize: {width: 1024, height: 768},
    verbose: true,
    logLevel: 'debug',
    onError: function(self, m) {   // Any "error" level message will be written
        console.log('FATAL:' + m); // on the console output and PhantomJS will
        self.exit();               // terminate
    }
});
casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
casper.start('http://casperjs.org/', function() {
    var pageText = this.evaluate(function() {
        return document;
    });

    if(pageText.indexOf('Casper') > -1) {
        console.log("Already logged in");
    } else {
        console.log("Not logged in");
    }

});
//
casper.run();

我收到以下输出:

casperjs test.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://casperjs.org/, HTTP GET
[debug] [phantom] Navigation requested: url=http://casperjs.org/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://casperjs.org/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://casperjs.org/ (HTTP 200)
TypeError: 'undefined' is not a function (evaluating 'pageText.indexOf('Casper')')
  C:/wamp/www/tw/tw.js:16
  C:/casperjs/modules/casper.js:1553 in runStep
  C:/casperjs/modules/casper.js:399 in checkStep

有人可以告诉我为什么它无法评估pageText.indexOf('Casper')吗?我不知道:(

1 个答案:

答案 0 :(得分:0)

事实上。 pageText在那里未定义。谢谢。这是一个虚假的问题。遗憾。