2016-02-29 47 views
0

我有关于使用casperjs jQuery的问题,我的代码是在这里:为什么我不能在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" 

回答

0

$('title').text();没有自己网站的控制台上工作,所以这个问题是不是在你的CasperJS测试。

您似乎没有在网站上使用JQuery,您的$函数是别的。

+0

谢谢你的回答,我开始使用nightmarejs – studentech

相关问题