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?