casperJS failed Injecting jQuery

旧巷老猫 提交于 2019-12-10 11:27:22

问题


Does anyone know how come I am encountering

[warning] [phantom] Failed injecting %s client side. 
Failed injecting includes/jquery-1.10.2.min.js client side 

when I have included

'includes/jquery-1.10.2.min.js' 

within the Casper constructor. Someone posted a similar question here: https://groups.google.com/forum/#!msg/casperjs/hY4ziaoXIEE/YFi8Sj4JysMJ, but I do not understand how they have incorporated the casper.evaluate() in their solution:

casper.then( function() {
this.evaluate(function($) {
console.log($('title').text());
}
}); 

回答1:


I don't remember ever being able to inject scripts using the clientScripts option of the CasperJs constructor. Instead I have found the following works for me always.

casper = require('casper').create();
casper.start();
casper.open('some url');
casper.then(function doSomething() {
    this.page.injectJs('relative/local/path/to/jquery.js');
    var items = this.evaluate(function () {
        return $('div.someClass'); // jquery here
    });
});



回答2:


What tripped me up was that the path to the include is relative to the directory you are calling the script from, NOT the directory the script actually resides in.



来源:https://stackoverflow.com/questions/17893243/casperjs-failed-injecting-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!