In the past, I used two different ways, including the page.evaluate () method, which queries the DOM that Declan mentions. Another way I passed the information from the webpage is to push it into console.log (), and in phantomjs script use:
page.onConsoleMessage = function (msg, line, source) { console.log('console [' +source +':' +line +']> ' +msg); }
I could also grab the msg variable in onConsoleMessage and look for some encapsulation data. Depends on how you want to use the output.
Then in the Nodejs script you will need to scan the output of the Phantomjs script:
var yourfunc = function(...params...) { var phantom = spawn('phantomjs', [...args]); phantom.stdout.setEncoding('utf8'); phantom.stdout.on('data', function(data) { //parse or echo data var str_phantom_output = data.toString(); // The above will get triggered one or more times, so you'll need to // add code to parse for whatever info you're expecting from the browser }); phantom.stderr.on('data', function(data) { // do something with error data }); phantom.on('exit', function(code) { if (code !== 0) { // console.log('phantomjs exited with code ' +code); } else { // clean exit: do something else such as a passed-in callback } }); }
Hope this helps.
ultrageek May 31 '12 at 20:21 2012-05-31 20:21
source share