6

I am trying to render a a javascript evaluated source code from a website using phantomjs. But every time I try i only get the source code as is (similar to view source from the browser). What I actually want is the javascript evaluated code (what we see from inspect element from google chrome browser). My code looks like this:

var page = require('webpage').create();
page.open('http://www.google.com/', function (s) {
    console.log(page.content);
    phantom.exit();
});

Am I doing something wrong here?

Trancey
  • 699
  • 1
  • 8
  • 18

2 Answers2

5

This did the trick for me:

window.setTimeout(function () {
        page.render(output);
        phantom.exit();
    }, 1000);
James Gentes
  • 7,528
  • 7
  • 44
  • 64
3

Likely yes. In many cases, the JavaScript code on the web page is not executed immediately. You can take this into account by giving a little delay, e.g. using setTimeout, before taking the value of page.content.

Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
  • 2
    any suggestions how to go about it? Any small sample code would help, maybe you are right, some sites do generate the javascript contents but mostly they dont. Also unable to produce results after a click and load. Any help in this regard really appreciated !! Thanks for replying – Trancey Mar 05 '13 at 11:31