I am using the following code to take screenshot of a page using phantomjs. It works perfectly for most of the pages but shows a "Operation canceled" error for some URLs.
var page = require('webpage').create(),
system = require('system');
page.viewportSize = {
width: 1366,
height: 800
};
page.onConsoleMessage = function(msg) {
console.log("CONSOLE MESSAGE:" + msg);
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open(<URL>, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
phantom.exit();
} else {
page.render("3.png");
phantom.exit();
}
});
console.log("opening");
Some of the examples of URLs not working are: http://india.gov.in
, http://timesofindia.indiatimes.com/
etc.
For india.gov.in
the first request to the url: http://india.gov.in/ is being resend midway and all the existing operations get cancelled just before it. Similar things happens with some other URLs as well.
Is the page getting refreshed? If yes how do i handle it?
How to follow redirects/reloads in phantomjs?
Further investigation gave me some more very interesting stats. During india.gov.in, phantomjs started sending requests and receiving responses at one point (after making request id: 118
) the number of pending requests was 37
. Phantom 'canceled' the pending requests and started loading page from the beginning. It started sending requests again.
After request id 156
the number of pending requests was again 37
(it received response only to one request this time). So it again 'canceled' all the pending requests are started again.
After request id 194
the number of pending requests was again 37
so it again canceled and restarted.
After request id 232
the number of pending requests was again 37
but this time it didn't restarted and canceling.
Is there a limit to number of pending requests that phantom can handle?
Thanx!!
Note! Note! Note!: It worked with phantomjs 2 right out of the box. But since that is still in development, it can't be used in my production setup. Does anyone has any idea which updation between 1.9.8 and 2.0 fixed it? I could probably import that change in 1.9.8's source and build it.