我在 C:\xampp\htdocs\phantom 中安装了 PhantomJS,并且在该文件夹 C:\xampp\htdocs\casper 中安装了 CasperJS
当我尝试使用 casper 站点上运行这些示例代码时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();
它给了我一个错误告诉我:
错误:找不到模块“casper”
我做错了什么?
如果你想通过 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;
一切都来自这个问题:在 phantom 中运行“casperjs 测试”
尽管这是可能的,但您不应该这样做。您应该通过 node_modules/casperjs/batchbin 中的可执行文件/批处理文件直接调用 CasperJS。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)