我在C:\xampp\htdocs\幻影中安装了PhantomJS,在这个文件夹C:\xampp\htdocs\casper中安装了CasperJS。
当我尝试使用phantomjs test.js命令在casper站点上运行这些示例代码时:
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();它给了我一个错误,告诉我:
错误:找不到模块“casper”
我做错什么了?
发布于 2014-09-10 09:30:31
如果您想通过CasperJS运行PhantomJS (因为您调用了phantomjs test.js),您需要在脚本开始时使用一些引导代码:
phantom.casperPath = 'path/to/node_modules/casperjs';
phantom.injectJs('path/to/node_modules/casperjs/bin/bootstrap.js');请记住,即使在windows上,也需要使用正斜杠。
如果您需要测试环境,那么还需要行:
phantom.casperTest = true;所有内容都来自于这个问题:Running 'casperjs test' in phantom
虽然这是可能的,但你不应该这么做。您应该通过节点_模块/ CasperJS /batchbin中的可执行/批处理文件直接调用casperjs。
发布于 2014-09-10 09:16:38
好的,我知道我做错了什么--我在用于casperjs的路径上犯了一个错误,我应该使用"C:\xampp\htdocs\casper\batchbin“而不是"C:\xampp\htdocs\casper\bin”。我不会删除这篇文章,这可能会帮助其他新手,就像我一样。
发布于 2018-07-06 06:53:58
您应该通过以下命令行运行您的程序:
casperjs test.jshttps://stackoverflow.com/questions/25761028
复制相似问题