为什么可以'我在Casperjs中使用indexOf

Why can't I use indexOf in Casperjs?

本文关键字:Casperjs indexOf 我在 为什么      更新时间:2023-09-26

我有以下代码(试图用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('Cancer')吗?我不知道:(

确实如此。pageText在那里未定义。非常感谢。这是一个伪问题。很抱歉