我在C:\xampp\htdocs\phantom中安装了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();
它给我一个错误,告诉我:
我做错了什么?
最佳答案
如果要通过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;
一切都取自这个问题:Running 'casperjs test' in phantom
尽管这是可能的,但您不应该这样做。您应该直接通过node_modules/casperjs/batchbin中的可执行文件/批处理文件直接调用CasperJS。
关于phantomjs - 通过PhantomJS调用时找不到模块 'casper',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25761028/