PNG is not being rendered using PhantomJS with multiple attempts in a loop

var page = require('webpage').create();
var filepath = "/home/abc/" + "abc.html"; //create path to charts.html    
for (i = 0; i < 3; i++) { // make three attempts
    console.log("hi"), function(status) {
      //if(status !== 'success') { //if not success, make another attempt
//        continue;
  //    }


running this code like this:

$ phantomjs hello.js

Q1: I am expecting abc.png to be generated in my home dir. However with current code it is not generated.

Q2: Adding the commented out if block back causes the code execution to become stuck. Why so ?



You have three problems that are intertwined.

1. continue

continue is used to go to the next iteration of a loop construct such as for or while. That's not what you're doing here. There is no loop around continue in the local scope which means this is a syntax error.

There is a bug in PhantomJS 2 that doesn't show Syntax errors anymore and simply freezes. Run your favorite linter (jslint, jshint, eslint) to prevent such mistakes.

2. is asynchronous

You cannot use a loop to open multiple page one after another. is asynchronous. By calling it, you only initiate the load. Since it doesn't block on the next iteration you will overwrite the request with a new URL and the first request will never be completed, because a for-loop is much faster than a page request.

The only page that could potentially be loaded is the last one, but ...

3. Premature exiting

You're exiting too early. Since is asynchronous, the loop will run completely through without loading even one page completely. At the end you have a phantom.exit() which will exit PhantomJS immediately.

If you have to open a bunch of pages, use a recursive approach or a clever use of setTimeout(). You could also use the async's series() function to write it in a more sane way.

Example as a simple recursive function:

var page = require('webpage').create();
var filepath = "/home/abc/" + "abc.html"; //create path to charts.html    
function run(i){
    if (i <= 0) {
      return phantom.exit();
    }, function(status) {
      if(status === 'success') { //if not success, make another attempt


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.