0

I am trying to get screenshot of some webpages using PhantomJS 2.0. However, I need the script runs just after the webpage is loaded completely.

Ex.: In this wepage http://esed.nl/druk-en-printwerk/esed-druk-en-printwerk/verpakkingsdrukwerk every time I get the screen the result it's just a blank image with a loading red gif. Like it

This is my code.

#!/usr/bin/env phantomjs 
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000, //< Default Max Timout is 5s
    start = new Date().getTime(),
    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()), //< defensive code
    interval = setInterval(function() {
        if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
            // If not time-out yet and condition not yet fulfilled
            condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
        } else {
            if(!condition) {
                // If condition still not fulfilled (timeout but condition is 'false')
                console.log("'waitFor()' timeout");
                phantom.exit(1);
            } else {
                // Condition fulfilled (timeout and/or condition is 'true')
                console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                clearInterval(interval); //< Stop this interval
            }
        }
    }, 500); //repeat check every 500ms
};

var system = require('system')
var width = window.parseInt(system.args[1])
var height = window.parseInt(system.args[2])
var url = system.args[3]
var relative_path = system.args[4]

var page = require('webpage').create();
var resources = [];

page.onResourceRequested = function(request) {
  resources[request.id] = request.stage;
};

page.onResourceReceived = function(response) {
  resources[response.id] = response.stage;
};

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

//Loading whole webpage
page.viewportSize = {
  width: 2000,
  height: 10000
};

page.open(url, function(status) {
  if (status !== 'success') {
    console.log("Error opening url \"" + page.reason_url + "\": " +                  page.reason );
  } else {
    waitFor(function() {
      page.viewportSize = { width: width, height: height };
      page.render(relative_path);
    }, 10000);
  }
  phantom.exit();
});

So, I need the screen when the page is completely loaded. Someone has any idea how could I do it?

Kelyane
  • 467
  • 2
  • 7
  • 19
  • Your use of `waitFor` is not correct. You don't have a test function or your test function returns `undefined`. This will run for 10 seconds and complain that the condition wasn't fulfilled. This will also write the screenshot every 250ms. When you look at the screenshot, does it still show a loading thingy? What happens when you exchange `waitFor` with `setTimeout`? – Artjom B. Nov 12 '15 at 13:44
  • Which PhantomJS version do you use? Please register to the `onConsoleMessage`, `onError`, `onResourceError`, `onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js)). Maybe there are errors. – Artjom B. Nov 12 '15 at 13:44
  • Hi! When I change the `waitFor` to `setTimeout` with 5000ms in this same code, nothing happens. Howerver in a sample code the image result it's that I want, but I can't wait this time in every page I will get the screen. I need something more automatic. – Kelyane Nov 12 '15 at 14:01
  • By the way, you're exiting too early. `waitFor` and `setTimeout` are asynchronous, so you need to move the `phantom.exit()` call inside. – Artjom B. Nov 12 '15 at 14:15
  • If I remove the `phantom.exit()` happens `waitFor() timeout`. and if I change the place inside the `waitFor()` nothing change. – Kelyane Nov 12 '15 at 14:29
  • I already saw this page, but it didn't help me. – Kelyane Nov 12 '15 at 14:31
  • As I said in the first comment. Your use of `waitFor` is wrong. If you saw that linked question then you should have seen [this answer](http://stackoverflow.com/a/14748934/1816580) on that question which show you the correct usage. – Artjom B. Nov 12 '15 at 14:34

0 Answers0