Javascript 如何在节点js上链接数量可变的方法?

Javascript 如何在节点js上链接数量可变的方法?,javascript,node.js,nightmare,Javascript,Node.js,Nightmare,我在用噩梦来破坏一个网站。我想根据一些输入链接多个操作(承诺?)。以下面的代码为例: var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true }); nightmare .goto('https://www.servipag.com/') .select('select#servicios.txt_formulario', '29') .wait(200) .

我在用噩梦来破坏一个网站。我想根据一些输入链接多个操作(承诺?)。以下面的代码为例:

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);
   });
我希望能够附加以下行的可变次数(从1到15):

   .select('select#servicios.txt_formulario', '29')
   .wait(200)
   .select('select#billers', '700')
   .insert('input#identificador','60957924')
   .click('#formPagoCuentas a[href^="javascript:AgregarCuentasaPagar"]')
   .wait(10)
因此,四次的总体代码是:

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);
   });

我该怎么做呢?

我对噩梦一无所知,但看起来一切都在排队等待 您调用end,它只返回自身进行链接。所以这应该行得通

  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);
     });
var operations=dream.goto('https://www.servipag.com/');
对于(变量i=0;i<4;i++){
操作=操作
.select('select#servicios.txt_formulario','29')
.等等(200)
.select('select#billers','700')
.insert('input#Identificator','60957924')
。单击(“#FormPagDocumentas a[href^=“javascript:AgregarcentaSapagar”]”)
.等待(10);
}
操作
。单击(“#FormPagDocumentas a[href^=“javascript:enviar”]”)
.wait('fieldset'))
.评估(功能){
return document.querySelector('.txt_detalle_boleta').innerHTML;
})
(完)
.然后(函数(结果){
控制台日志(结果);
})
.catch(函数(错误){
console.error('搜索失败:',错误);
});

您可以使用
reduce
来保持链运行:

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) );
请注意,
reduce
的第二个参数提供了“循环”之前的初始值,因此,
dream.goto…
就是在这里进行的