How to use jQuery with phantomjs -
i'm trying simple test of jquery phantomjs:
var url = 'http://www.google.com/'; var page = require('webpage').create(); page.open(url, function () { page.injectjs('jquery.min.js'); page.evaluate(function () { console.log($('title').text()); }); phantom.exit() });
this should print out title, instead nothing happens. can tell me what's wrong?
page.evaluate
executes function in page context, not in phantomjs context. so, uses console
printed (and visible) current web page instance.
to redirect console
output, must use onconsolemessage
callback.
var url = 'http://www.google.com/'; var page = require('webpage').create(); page.onconsolemessage = function(msg) { console.log(msg); }; page.open(url, function () { page.injectjs('jquery.min.js'); page.evaluate(function () { console.log($('title').text()); }); phantom.exit() });
reference: onconsolemessage
Comments
Post a Comment