0

I'm using Nightmarejs to scrap a website. I want to chain multiple operations (promises?) depending on some input. Take the following code:

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

nightmare
   .goto('https://www.servipag.com/')
   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
   .click('#formPagoCuentas a[href^="javascript:enviar"]')
   .wait('fieldset')
   .evaluate(function () {
     return document.querySelector('.txt_detalle_boleta').innerHTML;
   })
   .end()
   .then(function (result) {
     console.log(result);
   })
   .catch(function (error) {
     console.error('Search failed:', error);
   });

I want to be able to attach a variable amount of times (from 1 to 15) the following lines:

   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)

so the overall code for four times would be:

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

nightmare
   .goto('https://www.servipag.com/')
   // -- repeat this 4 times
   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
   // ---
   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
   // ---
   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
   // ---
   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
   // -- end
   .click('#formPagoCuentas a[href^="javascript:enviar"]')
   .wait('fieldset')
   .evaluate(function () {
     return document.querySelector('.txt_detalle_boleta').innerHTML;
   })
   .end()
   .then(function (result) {
     console.log(result);
   })
   .catch(function (error) {
     console.error('Search failed:', error);
   });

How can I do it?

2 Answers 2

1

I know nothing about nightmare, but it looks like everything is queued until you call end and it just returns itself for chaining. So this should work

  var operations = nightmare.goto('https://www.servipag.com/');

  for(var i = 0; i < 4; i++) {
     operations = operations
        .select('select#servicios.txt_formulario', '29')
        .wait(200)
        .select('select#billers', '700')
        .insert('input#identificador','60957924')
        .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
        .wait(10);
  }

  operations
     .click('#formPagoCuentas a[href^="javascript:enviar"]')
     .wait('fieldset')
     .evaluate(function () {
       return document.querySelector('.txt_detalle_boleta').innerHTML;
     })
     .end()
     .then(function (result) {
       console.log(result);
     })
     .catch(function (error) {
       console.error('Search failed:', error);
     });
Sign up to request clarification or add additional context in comments.

Comments

0

You could use reduce to keep the chain going:

Array(4).fill().reduce( acc =>
    acc.select('select#servicios.txt_formulario', '29')
       .wait(200)
       .select('select#billers', '700')
       .insert('input#identificador','60957924')
       .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
       .wait(10),
    nightmare.goto('https://www.servipag.com/') )
   .click('#formPagoCuentas a[href^="javascript:enviar"]')
   .wait('fieldset')
   .evaluate( _ => document.querySelector('.txt_detalle_boleta').innerHTML )
   .end()
   .then( result => console.log(result) )
   .catch( error => console.error('Search failed:', error) );

Note that the second argument to reduce provides the initial value before the "loop", so that is where nightmare.goto ... goes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.