Injecting & using Firebug-Lite with PhantomJS

冷暖自知 提交于 2019-12-03 21:51:36

You've got some confusion here around the context of each function:

  • When you run includeJs(url, callback), the callback function runs in the PhantomJS context. So it has access to page, but does not have access to variables and namespaces created by the included script - these were included in the client context, and will be available to functions you run through page.evaluate().

  • You're trying to run page.injectJs() within page.evaluate(). This won't work - the function you run in page.evaluate() is sandboxed in the client execution context, and does not have access to the page object.

So you should try one of these approaches - either run page.evaluate() in the page.includeJs() callback:

    page.includeJs("http://getfirebug.com/releases/lite/1.4/firebug-lite.js", 
        function () {
            page.evaluate(function() {
                // do stuff with firebug lite here
                console.log(inspect($("*")[5]));
            });
        });

or run page.injectJs() and then run page.evaluate():

// note - this is a reference to a local file
page.injectJs("firebug-lite.js");
page.evaluate(function() {
    // do stuff with firebug lite here
    console.log(inspect($("*")[5]));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!