PhantomJs:Spawn没有打开任何应用程序

PhantomJs: Spawn is not opening any app

本文关键字:任何 应用程序 Spawn PhantomJs      更新时间:2023-09-26

我全局使用npm安装了phantomJs。为什么这个代码不起作用?

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();
  }
})

我使用的是linux mint。在终端中键入命令/usr/bin/sensible-browser file:///example.png可以很好地工作,但为什么这不能通过脚本工作呢。?

原来phantom.exit()在spawn()完成之前就被调用了。以下代码修复了问题。

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);
});

参考:https://github.com/ariya/phantomjs/pull/14220

相关文章: