Roy
Roy

Reputation: 945

'null' is not an object error when including jQuery into an opened page using PhantomJS

I'm trying out PhantomJS and wanted to use it to extract content from a webpage. However, I cannot inject jQuery using the following code.

console.log('SSL support = ', require('system').isSSLSupported);

var page = require('webpage').create();
console.log('page created');
page.open('https://www.google.com/#q=my+test+query', function() {
  console.log('page opened');
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", function() {
    console.log('jQuery injected');
    phantom.exit();
  });
});

When I run the code, I see an error

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
TypeError: 'null' is not an object (evaluating 'document.body.appendChild')

  http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js:1
^C
C:\Users\royshi\SkyDrive\Developer\Crawler>

My code is not too different from the example provided by PhantomJS. Wondering what could be wrong here.

EDIT:

It seems there is something wrong with the URL I used in the above example (i.e. "https://www.google.com/#q=my+test+query"). The code works for another URL: https://www.google.com/:

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
jQuery injected

C:\Users\royshi\SkyDrive\Developer\Crawler>

Wondering what's wrong with the URL "https://www.google.com/#q=my+test+query".

Upvotes: 3

Views: 1689

Answers (1)

nietonfir
nietonfir

Reputation: 4881

Answering your edit as you got your solution: The hashtag triggers a redirect. As I don't know PhantomJS I suspect you need to follow it manually like this gist suggests.

Upvotes: 2

Related Questions