如何在node.js中使用CasperJS? [英] How to Use CasperJS in node.js?

查看:212
本文介绍了如何在node.js中使用CasperJS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在node.js中使用CasperJS.

I would like to use CasperJS in node.js.

我已经参考以下URL在node.js中使用CasperJS:

I have referred to the following URL's to use CasperJS in node.js:

  • https://github.com/sgentle/phantomjs-node
  • http://casperjs.org/index.html#faq-executable

借助上述URL,我编写了以下代码:

With the help of the above URLs I have written the following code:

//DISPLAY=:0 node test2.js
var phantom = require('phantom');
console.log('Hello, world!');
phantom.create(function (ph) {
    ph.casperPath = '/opt/libs/casperjs'
    ph.injectJs('/opt/libs/casperjs/bin/bootstrap.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();
    }, {
        term: '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();
});


运行此代码时,出现以下错误:


When I run this code, I got the following error:

错误MSG:

tz@tz-ubuntu:/opt/workspaces/TestPhantomjs$ DISPLAY=:0 node test2.js 
Hello, world!
Error: Cannot find module 'casper'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at /opt/workspaces/TestPhantomjs/test2.js:6:14
    at Object.<anonymous> (/opt/workspaces/TestPhantomjs/node_modules/phantom/phantom.js:82:43)
    at EventEmitter.<anonymous> (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode/index.js:215:30)
    at EventEmitter.emit (events.js:67:17)
    at handleMethods (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:138:14)
    at EventEmitter.handle (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:98:13)
phantom stdout: Unable to load casper environment: Error: Failed to resolve module fs, tried fs

推荐答案

尼古拉斯·佩里奥(Nicolas Perriault)
2012/2/27天猪蓝虫. :

Nicolas Perriault
2012/2/27 天猪 蓝虫. :

我想在nodejs中使用casperjs. 并引用: https://github.com/sgentle/phantomjs-node http://casperjs.org/index.html#faq-executable

I wan to use casperjs in nodejs. and refs to: https://github.com/sgentle/phantomjs-node and http://casperjs.org/index.html#faq-executable

您不能以这种方式运行CasperJS. QtWebKit和V8不共享相同的内容 js环境(和事件循环),因此您的node.js应用将无法 加载并使用CasperJS模块.您必须运行CasperJS脚本 分别使用子流程调用在github上进行此调用.一世 不打算使CasperJS与phantomjs-node兼容,因为它 使用我不太容易使用的基于alert()的肮脏黑客.

You can't run CasperJS that way; QtWebKit and V8 don't share the same js environment (and event loop), so your node.js app won't be able to load and use a CasperJS module. You have to run your CasperJS script separately using a subprocess call, like this one on github. I don't plan to make CasperJS compatible with phantomjs-node because it uses alert()-based dirty hacks I'm not easy with.

干杯, -尼古拉斯·佩里奥(Nicolas Perriault)

Cheers, -- Nicolas Perriault

这篇关于如何在node.js中使用CasperJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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