为什么我不能在casperjs中使用jquery?

时间:2016-02-29 09:46:58

标签: jquery phantomjs casperjs

我对在casperjs中使用jquery有疑问,我的代码在这里:

var fs = require('fs');
var casper = require('casper').create({
    clientScripts: ["./public/javascripts/jquery-1.11.1.min.js"]
    verbose:false,
    logLevel:'debug',
    pageSettings: {
        loadPlugins:true,
            loadImages:  false, 
    }
});
//phantom.outputEncoding = 'gbk';
var url = 'http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29';
casper.userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4');

//获取需要采集的url列表
casper.start(url,function(){    
    casper.GetDetailUrl(url);
});

//打开具体url
casper.GetDetailUrl = function(detailUrl){
        casper.thenOpen(detailUrl,function(){
        console.log('[URL:]'+ this.getCurrentUrl());
    })
};

//处理具体页面
casper.then(function getDate() {
     product = casper.evaluate(function getDateFromPage(){
        //return document.title;
        return $('title').text();
    });

    this.echo(product);

    var item = new Object();
    item.title = product;
    item.murl = this.getCurrentUrl();
    //casper.PostData(item);
});
casper.run();

我明白了:

[URL:]http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29
null

但是,当我使用时:

return document.title;

结果是对的,为什么?

这是我的调试信息。我发现jquery注入成功,除了当我在casper.start(){}块中使用jquery时,它正常工作....

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29, HTTP GET
[debug] [phantom] Navigation requested: url=http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29"
[debug] [phantom] Automatically injected ./public/javascripts/jquery-1.11.1.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29 (HTTP 200)
[info] [phantom] Step anonymous 2/3: done in 321ms.
[debug] [phantom] opening url: http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29, HTTP GET
[debug] [phantom] Navigation requested: url=http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29"
[debug] [phantom] Automatically injected ./public/javascripts/jquery-1.11.1.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/5 http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29 (HTTP 200)
[URL:]http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29
[info] [phantom] Step anonymous 4/5: done in 442ms.
[info] [phantom] Step getDate 5/5 http://m.gewara.com/movie/m/choiceMovieSim.xhtml?cid=37950723&mid=278455503&openDate=2016-02-29 (HTTP 200)
null
[info] [phantom] Step getDate 5/5: done in 464ms.
[info] [phantom] Done 5 steps in 481ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

1 个答案:

答案 0 :(得分:0)

$('title').text();无法在您网站的控制台上运行,因此问题不在您的CasperJS测试中。

您似乎没有网站上的JQuery,您的$功能是其他功能。