8

I installed PhantomJS in C:\xampp\htdocs\phantom and also I installed CasperJS in this folder C:\xampp\htdocs\casper

When I tried to run these sample code on casper site using phantomjs test.js command:

var casper=require('casper').create();
casper.start('http://google.fr/');

casper.thenEvaluate(function(term) {
    document.querySelector('input[name="q"]').setAttribute('value', term);
    document.querySelector('form[name="f"]').submit();
}, 'CasperJS');

casper.then(function() {
    // Click on 1st result link
    this.click('h3.r a');
});

casper.then(function() {
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});

casper.run();

It gives me an error telling me that:

Error: Cannot find module 'casper'

What did I do wrong?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
chitcharonko
  • 331
  • 2
  • 6
  • 18

5 Answers5

7

If you want to run CasperJS through PhantomJS (since you invoke phantomjs test.js) you need some bootstrapping code at the beginning of the script:

phantom.casperPath = 'path/to/node_modules/casperjs';
phantom.injectJs('path/to/node_modules/casperjs/bin/bootstrap.js');

Keep in mind that even on windows, you need to use a forward slash.

If you need the test environment then you also need the line:

phantom.casperTest = true;

Everything is taken from this question: Running 'casperjs test' in phantom

Although this is possible you shouldn't do it. You should invoke CasperJS directly through its executable/batch file in node_modules/casperjs/batchbin.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
2

okay i know what i did wrong i had a mistake on the path i used for casperjs, i should have used "C:\xampp\htdocs\casper\batchbin" instead of "C:\xampp\htdocs\casper\bin". i will not delete this post, this might help other newbies to casperjs just like me.

chitcharonko
  • 331
  • 2
  • 6
  • 18
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Zo Has Sep 10 '14 at 09:47
  • 2
    @DamienJoe This is an answer with a little commentary around it. – Artjom B. Sep 10 '14 at 09:49
2

You should run your program by the following command line:

casperjs test.js
Thomas Choy
  • 321
  • 2
  • 13
0

The error issue is with the path you have install the casper.js and phantom.js

//I am indicating the installation directory of casper.js
phantom.casperPath = '/usr/local/share/casperjs';

// indicating the inner directory of casper js which contains bootstrap.
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');

I hope it will be helpful.

balintbabics
  • 1,291
  • 2
  • 11
  • 25
Active
  • 13
  • 3
0

If you already have installed casperjs locally I think you can run it directly -> MacOs:

$ casperjs sample.js