我正在使用 Nightmarejs 废弃一个网站。我想根据某些输入链接多个操作( promise ?)。采取以下代码:
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);
});
我该怎么做?
最佳答案
我对 Nightmare 一无所知,但看起来一切都在排队,直到 你调用 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);
});
关于javascript - 如何在 Node js 上链接可变数量的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42147054/