I am trying to take a screenshot of a webpage with PhantomJS. Specifically, I am using the example of capturing espn.com
from this example. My code looks like this:
var page = new WebPage();
page.open('http://www.espn.com', function (status) {
page.render('fb.png');
phantom.exit();
});
I then go to my PhantomJS directory with either my terminal or command prompt and run:
phantomjs shotty.js
Everything runs great, however it takes 6-8 seconds to complete the output image. Is that normal? Is there a faster way to accomplish this so that it completes in a second or less?
I am using CentOS and Windows 7. Both boxes have 8GB of RAM, 3.2 GHz CPU, and I'm getting 22Mbp/s down and 1Mbp/s up on speedtest.net
Well, in my case, the page was waiting for some GET requests and was not able to reach the requests' server and it kept waiting for long. I could only figure it out when i used the remote debugger option.
phantomjs --remote-debugger-port=9000 loadspeed.js <some_url>
and inside the loadspeed.js
file add this below code:
page.onResourceRequested = function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
};
page.onResourceReceived = function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
};
and then loading localhost:9000 in any webkit browser (safari/chrome) and seeing the console logs where i could figure out it was waiting for some unreachable requests for a long time.
TO BYPASS THIS - REDUCE THE TIMEOUT by adding below to the same loadspeed.js
file:
page.settings.resourceTimeout = 3000; //in milliseconds
and things were very quick after that. Hope this helps