Search code examples
javascriptcronnightmare

Cron and nightmare js


I am trying to run a cron with nightmare js locally. Unfortunately I have this error.

Unhandled rejection (<{"message":"navigation error","code":-...>, no stack trace)

Related issue : Nightmare JS not working

I was wondering if it is linked with the fact that nightmare require a graphical interface?

Thanks for your help,

EDIT

In my cron, I have one Promise function which is composed of the cron followed by promises.

var job = new CronJob('* */10 * * * *', function() {
    crawl()
  }, function () {
    console.log("crawl ended")
  },
  true
);


job.start();

Here is what nightmare looks like:

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });

When I run the function crawl without cron it works great.


Solution

  • My issue was in the settings of the cron. I'd rather use

    var job = new CronJob('* 10 * * * *', function() {
        crawl()
      }, function () {
        console.log("crawl ended")
      },
      true
    );
    

    Plus I had to redefine nightmare settings INTO my function.

    var get_data = function(){
      var Nightmare = require('nightmare');
      var nightmare = Nightmare({
        typeInterval: 300,
        show: true
      });
      nightmare
      .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
      .type('[name=email]', '')
      .wait(1000)
      .type('[name=email]', 'myemail')
      .wait(1000)
      .type('[name=password]', '')
      .wait(1000)
      .type('[name=password]', 'mypassword')
      .click('[type=submit]')
      .wait(25000)
      .wait(25000)
      .evaluate(function (page, done) {
    
        document.documentElement
        done()
      })
      .end()
      .then(function (result) {
        // fs.writeFileSync('testOutput.json', JSON.stringify(result));
        console.log(JSON.stringify(result))
      })
      .catch(function (error) {
        console.error('failed:', error);
      });
    }
    

    Instead of

    var Nightmare = require('nightmare');
    var nightmare = Nightmare({
      typeInterval: 300,
      show: true
    });
    
    var get_data = function(){
      nightmare
      .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
      .type('[name=email]', '')
      .wait(1000)
      .type('[name=email]', 'myemail')
      .wait(1000)
      .type('[name=password]', '')
      .wait(1000)
      .type('[name=password]', 'mypassword')
      .click('[type=submit]')
      .wait(25000)
      .wait(25000)
      .evaluate(function (page, done) {
    
        document.documentElement
        done()
      })
      .end()
      .then(function (result) {
        // fs.writeFileSync('testOutput.json', JSON.stringify(result));
        console.log(JSON.stringify(result))
      })
      .catch(function (error) {
        console.error('failed:', error);
      });
    }