Cannot find "casper" module when called through PhantomJS

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

When I tried to run this sample code on casper using the phantomjs test.js :

 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(); 

This tells me the error:

Error: cannot find module "casper"

What have I done wrong?

+8
source share
5 answers

If you want to run CasperJS through PhantomJS (since you are calling phantomjs test.js ), you will need some boot 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 in windows you need to use a slash.

If you need a test environment, you will also need a line:

 phantom.casperTest = true; 

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

Although it is possible, you should not do this. You must invoke CasperJS directly through the executable / batch file in node_modules / casperjs / batchbin.

+7
source

OK, I know what I did wrong, I made a mistake on the path that I used for casperjs, I had to use "C: \ xampp \ htdocs \ casper \ batchbin" instead of "C: \ xampp \ htdocs \ casper \ bin" . I will not delete this message, it may help other newcomers to casperjs, like me.

+2
source

You must start your program using the following command line:

 casperjs test.js 
+2
source

The problem with the error is that you installed 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 this will be helpful.

0
source

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

$ casperjs sample.js

0
source

Source: https://habr.com/ru/post/975055/


All Articles