Newbie confusion - Try to use casperjs in nodejs and got some error

已查看 3,310 次
跳至第一个未读帖子

天猪 蓝虫.

未读,
2012年2月26日 20:46:352012/2/26
收件人 casp...@googlegroups.com
I wan to use casperjs in nodejs.

and refs to:



but I got a problem with this 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();
});

---------------------------------------
*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日 01:45:152012/2/27
收件人 casp...@googlegroups.com
2012/2/27 天猪 蓝虫. <liuy...@gmail.com>:

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

You can't run casperjs that way; qtwebkit and v8 don't share the same
js environment (and event loop), so your node app won't be able to
load and use a casperjs module.

You have to run your casper script separately using a subprocess call,
eg https://github.com/n1k0/nodetunes/blob/master/Cakefile#L41. I don't
plan to make casper compatible with phantomjs-node because it uses
alert() based dirty hacks I'm not easy with.

Cheers,

--
Nicolas Perriault
http://www.akei.com/http://prendreuncafe.com/
Mobile: +33 660 92 08 67 — Skype: nperriault

回复全部
回复作者
转发
0 个新帖子