找不到模块';卡斯珀';当通过PhantomJS调用时

找不到模块';卡斯珀';当通过PhantomJS调用时,phantomjs,casperjs,Phantomjs,Casperjs,我在C:\xampp\htdocs\phantom中安装了PhantomJS,也在C:\xampp\htdocs\casper文件夹中安装了CasperJS 当我尝试使用phantomjs test.js命令在casper站点上运行这些示例代码时: var casper=require('casper').create(); 卡斯珀,开始http://google.fr/'); casper.thenEvaluate(功能(术语){ document.querySelector('input[n

我在C:\xampp\htdocs\phantom中安装了PhantomJS,也在C:\xampp\htdocs\casper文件夹中安装了CasperJS

当我尝试使用
phantomjs test.js
命令在casper站点上运行这些示例代码时:

var casper=require('casper').create();
卡斯珀,开始http://google.fr/');
casper.thenEvaluate(功能(术语){
document.querySelector('input[name=“q”]')).setAttribute('value',term);
document.querySelector('form[name=“f”]”)。submit();
}"CasperJS",;
casper.then(函数(){
//点击第一个结果链接
点击('h3.r a');
});
casper.then(函数(){
log('单击确定,新位置为'+this.getCurrentUrl());
});
casper.run();
这给了我一个错误,告诉我:

错误:找不到模块“casper”


我做错了什么?

好吧,我知道我做错了什么,我在casperjs的路径上犯了一个错误,我应该用“C:\xampp\htdocs\casper\batchbin”而不是“C:\xampp\htdocs\casper\bin”。我不会删除这篇文章,这可能会帮助其他像我一样熟悉casperjs的新手。

如果你想通过PhantomJS运行casperjs(因为你调用了
PhantomJS test.js
),你需要一些引导代码在脚本的开头:

phantom.casperPath = 'path/to/node_modules/casperjs';
phantom.injectJs('path/to/node_modules/casperjs/bin/bootstrap.js');
请记住,即使在windows上,也需要使用正斜杠

如果需要测试环境,则还需要以下行:

phantom.casperTest = true;
所有内容都取自这个问题:


虽然这是可能的,但你不应该这样做。您应该通过node_modules/CasperJS/batchbin中的可执行文件/批处理文件直接调用CasperJS。

错误在于您安装casper.js和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');

我希望它会有帮助。

您应该通过以下命令行运行程序:

casperjs test.js

如果您已经在本地安装了casperjs,我认为您可以直接运行它->MacOs:


$casperjs sample.js

这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论——你可以随时在自己的帖子上发表评论,一旦你有足够的评论,你就可以发表评论了。@DamienJoe这是一个有一些评论的回答。