我将jQuery注入CasperJS:
phantom.injectJs('./utils/jquery/jquery-2.1.4.js');但是,当我试图计算一些jQuery代码时,它被忽略了:例如:
function dragNdropAlertToActivity() {
var tt = casper.evaluate(function() {
$('div[id^="scheduler-alert-grid"] table:contains(BLUE ALERT)')[0].simulate("drag-n-drop", {
dragTarget: {
dx: 71,
dy: 71,
interpolation: {
stepCount: 2
}
}
});
return "done";
});
casper.echo(tt);
};调用方法,如:casper.test.begin(function(){...})。测试使用:casperjs test tests执行
输出显示找不到$。
当我编写一个简单的选择器时,它为什么要忽略jQuery?
发布于 2015-07-22 15:11:04
phantom.injectJs()文档清楚地说明了(强调我的):
将指定文件的外部脚本代码注入幻影外部空间。
这意味着jQuery不会注入到尝试使用它的页面上下文中。
您可以使用手动方法(ref):
casper.then(function doSomething() {
this.page.injectJs('relative/local/path/to/jquery.js');
var tt = this.evaluate(function () {
// ...
});
});或者普通的CasperJS方法,它将脚本注入到您访问的每个页面上:
var casper = require("casper").create({
clientScripts: ["relative/local/path/to/jquery.js"]
});或
casper.options.clientScripts.push("relative/local/path/to/jquery.js");https://stackoverflow.com/questions/31566465
复制相似问题