0

I am trying to automate the process of downloading many webpage screenshots. All screenshots are on the same page, but different data come up based on the querystring parameter.

my code:

  var page = require('webpage').create();
  //How far the page extends in the image
  page.viewportSize = { width: 1024, height: 2500 };

  //size of the actual clip in the viewer (extra black space below image)
  page.clipRect = { top: 0, left: 0, width: 1024, height: 4500 };
console.log("configured");




for(var i = 1; i < 5; i++){


  page.open('http://testsite.com/view?id=' + i, function(status) {
    console.log("Status: " + status);
    if(status === "success") {

      //save image of page as this
      page.render(i + '.png');

    }
phantom.exit();


  });
}

This works without the for loop and just setting i to a random number. I have a feeling it has to do with the callback function and/or the placement of phantom.exit(). I have tried many variations and cannot get it to loop. I have also tried adding a custom synchronous delay function, but am not sure where this might be called. What would be the best way to automate this...time and resources are not an intensive issue, as this just needs to be run once to dump all the pictures to a file.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Your analysis is correct and there is an easy solution: recursion. I've close this question as a duplicate of another question which contains *one* possible solution. There are countless possible solutions. In your case, you can for example use your loop to populate the `urls` array from my answer in the duplicate question. – Artjom B. Aug 08 '16 at 18:59
  • @ArtjomB.Thank you! I have been looking for an answer like this and the one you link to about recursion for a while. Very complete explanation. – Ryan Gedwill Aug 08 '16 at 20:04

0 Answers0