Sergi Juanola
Sergi Juanola

Reputation: 6647

phantomjs doesn't render a page

I was doing some research in many websites today and, to avoid looking at them manually, I prepared phantomjs to render them using the solution proposed here. Nothing special. Looping through a website array and rendering all the resulting pages.

What's strange is that there are some websites that are not being properly rendered. Among others, I have this one: http://www.telegraaf.nl/

To simplify, I created another script that only runs this page:

var page = require('webpage').create();

page.viewportSize = { width: 1920, height: 960 };
page.clipRect = { top: 0, left: 0, width: 1920, height: 960 };

page.open('http://www.telegraaf.nl/', function(status) {
  page.render("screenshot.png");
  phantom.exit();
});

It ends in no screenshot. Tested with any other one, and perfectly working. Did I overlook something?

Upvotes: 3

Views: 6513

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

It doesn't render a screenshot, because the page has no <body> initially and therefore nothing to render. Everything, including the body, is loaded through JavaScript after PhantomJS' onLoadFinished event fires.

You need to wait a little for a full page load. A simple 5 second wait was sufficient for me:

page.open('http://www.telegraaf.nl/', function(status) {
    setTimeout(function(){
        page.render("screenshot.png");
        phantom.exit();
    }, 5000);
});

You can of course wait in a more fancy way in order to make it more robust and not to wait too long: phantomjs not waiting for “full” page load


You may need to run PhantomJS with --ignore-ssl-errors=true (and maybe --ssl-protocol=any if PhantomJS <1.9.8).

Upvotes: 8

Related Questions