Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantomjs - ReferenceError: Can't find variable: $

I have a PhantomJS script that works when I run it locally (Mac), but when I run it on my Linux server, it returns the following error:

ReferenceError: Can't find variable: $
https://fantasy.premierleague.com/a/statistics/value_form:5712 in global code

The code is:

var page = require('webpage').create();
var fs = require('fs');
var args = require('system').args;
page.settings.userAgent = 'SpecialAgent';

page.open('https://fantasy.premierleague.com/a/statistics/value_form', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function () {
        var result ="";
        // ...
        return result;
    });    

    }
    phantom.exit();
});
like image 276
p_mcp Avatar asked Sep 27 '16 02:09

p_mcp


1 Answers

There may be a race condition between your code and jQuery being loaded on the page. Wrap the statements in your page.evaluate callback with a $(document).ready(function() { /* your statements here */ }); to ensure scripts on the page have loaded fully.

like image 196
Stiliyan Avatar answered Oct 20 '22 00:10

Stiliyan