Search code examples
javascriptnode.jsphantomjscommand-line-interfacecasperjs

Call CasperJS from a script


I currently run a CasperJS script from CLI like so:

casperjs --ignore-ssl-errors=true --ssl-protocol=any scrape.js

.. but to automate the whole process I need to call it from another script as a module/function and pass parameters (keywords etc). So wondering whats the best way to do it.


Solution

  • Figured it. Spooky!

    var spooky = new Spooky({
      casper: {
        //configure casperjs here
      }
    }, function (err) {
      // NODE CONTEXT
      console.log('We in the Node context');
      spooky.start('http://www.example.com
      spooky.then(function() {
        // CASPERJS CONTEXT
        console.log('We in the CasperJS context');
        this.emit('consoleWe can also emit events here.');
        this.click('a#somelink  });
      spooky.then(function() {
        // CASPERJS CONTEXT
        var size = this.evaluate(function() {
        // PAGE CONTEXT
        console.log('....'); // DOES NOT GET PRINTED OUT
        __utils__.echo('We in the Page context'); // Gets printed out
        this.capture('screenshot.png');
        var $selectsize = $('select#myselectlist option').size();
          return $selectsize;
        })
      })
    

    source