I installed phantomJs using npm globally. Why this code isn't working?
var page = require('webpage').create(); var spawn = require('child_process').spawn; page.open('http://google.com', function(status){ if( status == 'success' ) { page.render('/tmp/google-snapshot.jpg'); spawn('/usr/bin/sensible-browser', 'file:///tmp/google-snapshot.jpg'); phantom.exit(); } })
Im using linux mint. Typing command /usr/bin/sensible-browser file:///example.png
in terminal works fine but why this doesn't work through script.?
It turns out phantom.exit() was getting called before spawn() can get complete. Below code fixed the problem.
var page = require('webpage').create();
var spawn = require('child_process').spawn;
page.open('http://google.com', function(status){
if( status == 'success' ) {
page.render('/tmp/google-snapshot.jpg');
spawn('/usr/bin/sensible-browser', 'file:///tmp/google-snapshot.jpg');
}
setTimeout(function(){
phantom.exit();
},2000);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.