Search code examples
phantomjsheadless

PhantomJS page.open freezes


I am trying to open a website using PhantomJS using the Capture example

var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

The code works fine on my local machine, using Ubuntu 14.04. But when I run the code on a virtual server, the callback is never called and the script does not exit. The server and my local machine are running the exact same setup. I tried it on 2 different servers on virtualised with VMWare and one with Virtuozzo.

It does not seem to be a network error as all requests are successful.


Solution

  • Actually the UserAgent caused all the trouble. @artjom-b pointed me to this Question Taking reliable screenshots of websites? Phantomjs and Casperjs both return empty screen shots on some websites. The Only difference is that for me the image file was never created.

    So setting the UserAgent with

    page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
    

    actually solved the problem.