我正在使用Nightmarejs废弃一个网站。我想根据一些输入链接多个操作(promises?)。请使用以下代码:
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);
});
我该怎么做?
答案 0 :(得分:1)
我对噩梦一无所知,但看起来一切都排好了 你打电话给结束,它只是为了链接而返回。所以这应该有用
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);
});
答案 1 :(得分:0)
您可以使用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
的第二个参数在&#34;循环&#34;之前提供了初始值,因此这是nightmare.goto ...
所在的位置。