user310291 user310291 - 3 months ago 20
Node.js Question

Why do my phantomjs script output no screenshot (command line args version)

I modified the basic phantomjs example here http://phantomjs.org/screen-capture.html to accept command line args.

When I pass http://google.com as argument console.log outputs are correct


0: index.js

1: http://google.com


but I don't get any thumbnail.png in my folder why ?

var page = require('webpage').create();

var system = require('system');
var args = system.args;

var url;

if (args.length === 1) {
url = 'http://github.com/';
} else {
args.forEach(function(arg, i) {
console.log(i + ': ' + arg);

if (i > 0) {
page.open(arg, function() {

page.render('thumbnail' + '.png');


});
}

});
}

phantom.exit();

Answer

page.open is an asynchronous function, therefore phantom.exit is being called before your callback to render the thumbnail.

move phantom.exit inside your callback as specified in the docs

var page = require('webpage').create();
  page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});