CasperJS doesn't evaluate jQuery method

别说谁变了你拦得住时间么 提交于 2019-12-25 04:48:12

问题


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

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