问题
I injected jQuery to CasperJS:
phantom.injectJs('./utils/jquery/jquery-2.1.4.js');
but when I try to evaluate some jQuery code, it is ignored: example:
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);
};
calling method like: casper.test.begin(function(){...})
.
test are executed using: casperjs test tests
The output says that $
cannot be found.
Why does it ignore jQuery, when I write even a simple selector?
回答1:
The phantom.injectJs() documentation clearly says (emphasis mine):
Injects external script code from the specified file into the Phantom outer space.
Which means that jQuery is not injected into the page context where you try to use it.
You can either use the manual approach (ref):
casper.then(function doSomething() {
this.page.injectJs('relative/local/path/to/jquery.js');
var tt = this.evaluate(function () {
// ...
});
});
or the normal CasperJS approach which injects the script on every page that you visit:
var casper = require("casper").create({
clientScripts: ["relative/local/path/to/jquery.js"]
});
or
casper.options.clientScripts.push("relative/local/path/to/jquery.js");
来源:https://stackoverflow.com/questions/31566465/casperjs-doesnt-evaluate-jquery-method