通过PhantomJS调用时找不到模块'casper'

时间:2014-09-10 08:47:17

标签: phantomjs casperjs

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

它告诉我一个错误:

  

错误:找不到模块'casper'

我做错了什么?

5 个答案:

答案 0 :(得分:7)

如果你想通过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。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

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

casperjs test.js

答案 3 :(得分:0)

错误问题在于您安装了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');

我希望它会有所帮助。

答案 4 :(得分:0)

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

$ casperjs sample.js