0

I am new to Casperjs, phantomjs .I have been trying hard to create some page automation to login and take some steps in a CMS but i am having issues with accessing the global window variables from casperjs evaluate() function. the below example is just checking jquery on Google. Jquery exists in the page and some other global functions but i can't access them from casperjs.

casper.start('https://www.google.ca/#hl=en', function() {

// search for 'casperjs' from google form
this.fill('form[action="/search"]', { q: 'casperjs' }, false);
});

casper.then(function() {
  this.evaluate(function jquery() {
      console.log('looking for jquery ---');
      console.log($ + 'exists');
  });
});

getting error - `ReferenceError: Can't find variable: $

How can i fix this ?

Any help is appreciated :)

  • If jQuery exists on the page, then you can use it through `evaluate` or `thenEvaluate`. Something else must be wrong. I see another problem, but it wouldn't result in this error: `this` refers to `window` inside of `evaluate`, so `this.capture` is actually `window.capture` and would result in an error itself. `casper` is not available in the page context. – Artjom B. Aug 28 '16 at 19:41
  • Google search doesn't have jQuery. `$` is something else. You really should not try to automate Google pages, because Google delivers different pages depending on the browser and it very well may be that they deliver a page without `$`, whatever that might be. – Artjom B. Aug 28 '16 at 21:46
  • @ArtjomB. Thanks. I found out after downgrading phantomjs version to 1.9.8 to see more errors which says fail to execute inline scripts because of Content security policy so those window objects don't get created – matharumanpreet00 Aug 28 '16 at 22:44
  • You can certainly run your script with the `--web-security=false` commandline option. – Artjom B. Aug 29 '16 at 05:05

1 Answers1

1

For use jQuery in casperjs inject script in page, something like:

var casper = require('casper').create({
    some code here,
    clientScripts:  ['/path/to/jquery.js'],
});
Alex Muravyov
  • 502
  • 6
  • 13