通过PhantomJS调用时找不到模块"casper" [英] Cannot find module 'casper' when invoking through PhantomJS

查看:101
本文介绍了通过PhantomJS调用时找不到模块"casper"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I installed PhantomJS in C:\xampp\htdocs\phantom and also I installed CasperJS in this folder C:\xampp\htdocs\casper

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

When I tried to run these sample code on casper site using phantomjs test.js command:

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"

Error: Cannot find module 'casper'

我做错了什么?

推荐答案

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

If you want to run CasperJS through PhantomJS (since you invoke phantomjs test.js) you need some bootstrapping code at the beginning of the script:

phantom.casperPath = 'path/to/node_modules/casperjs';
phantom.injectJs('path/to/node_modules/casperjs/bin/bootstrap.js');

请记住,即使在Windows上,也需要使用正斜杠.

Keep in mind that even on windows, you need to use a forward slash.

如果您需要测试环境,那么还需要以下行:

If you need the test environment then you also need the line:

phantom.casperTest = true;

一切都来自这个问题:在幻像中运行"casperjs测试"

Everything is taken from this question: Running 'casperjs test' in phantom

尽管这是可能的,但您不应该这样做.您应该直接通过node_modules/casperjs/batchbin中的可执行文件/批处理文件直接调用CasperJS.

Although this is possible you shouldn't do it. You should invoke CasperJS directly through its executable/batch file in node_modules/casperjs/batchbin.

这篇关于通过PhantomJS调用时找不到模块"casper"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆