console.log不会在casper.evaluate中打印

时间:2015-01-18 07:34:17

标签: javascript phantomjs casperjs

我正在尝试使用casper编写一个小刮刀,但我无法使评估功能正常工作。这是我的代码,

var casper = require('casper').create({
    clientScripts: ["bower_components/jquery/src/jquery.js"],
     verbose: true,
    logLevel: 'info'
});

casper.start('http://stackoverflow.com', function afterStart() {
    this.echo(this.getTitle());
    if (this.exists(".js-headword")) {
        //This gets executed
        this.echo("Meh!")
    }
    //If this is enabled the above code doesn't work. 

    // this.echo(this.fetchText(".js-headword"))
});
casper.then(function evalPage(){
    this.evaluate(function(){
        //This does not. 
        console.log("Evaluating title");
    })
})

casper.run();

这是我的casper日志

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[info] [phantom] Step afterStart 2/3 http://stackoverflow.com/ (HTTP 200)
Stack Overflow
Meh!
[info] [phantom] Step afterStart 2/3: done in 5960ms.
[info] [phantom] Step evalPage 3/3 http://stackoverflow.com/ (HTTP 200)
[info] [phantom] Step evalPage 3/3: done in 5982ms.
[info] [phantom] Done 3 steps in 6002ms

代码命中evalPage函数,但它不运行evaluate方法。从未到达console.log。我正在关注文档,完全不知道为什么这不起作用。

我在OSX10.10上运行了casper 1.1 beta 3.

编辑1:

我正在尝试在evaluate()中执行一些DOM操作。例如,如果页面有h1标签,我需要获取h1标签内的文本。

this.evaluate(function(){
    //This does NOT work

    console.log(document.querySelector("h1").textContent)

})

console.log是我正在尝试做的过于简化的版本。我对evaluate方法的理解是它在当前页面上下文中,我可以执行和DOM操作。但我无法做到这一点。

参考:http://casperjs.readthedocs.org/en/latest/modules/casper.html#evaluate

0 个答案:

没有答案
相关问题