使用 reduce 和 Promises 在 Nightmare js 中循环
Looping in Nightmare js using reduce and Promises
我正在尝试加载 1 URL,然后 select select 菜单中的所有项目,以下载多个银行对帐单。
我正在按照指南 here 使用 reduce 进行循环。
部分代码:
const statementOptions = [
'1:accepted:1',
'3:accepted:3',
'4:accepted:4',
...
]
nightmare.goto('https://www.homesavings.com')
//login
.viewport(900, 800)
.wait('form[name="Login"]')
.insert('#userid', config.userid)
.insert('#password', config.password)
.click('#submit input')
// go to Online Statements page
.wait('a[data-app-code="Online Statements"]')
.goto('https://www.hslonline2.com/onlineserv/HB/OnlineStatements.cgi')
.evaluate(statementOptions => statementOptions, statementOptions)
.then(statementOptions => {
return statementOptions.reduce(function(acc, option, index) {
console.log(`reduce it: option=${option} index=${index}`)
return acc.then(function(results) {
return nightmare
// wait for the account select box to load..
.wait('select[name="acctRef"]')
.select('select[name="acctRef"]', option)
//.wait(1000)
.click('input[type="submit"]')
//.wait(2000)
.wait('td a img')
.click('td a')
.download(`${downloadDir}/bankPDF_${index}`)
.then(info => {
console.log(`after download. info -> ${JSON.stringify(info, null, 2)}`)
results.push(info)
return results
})
})
}, Promise.resolve([])).then(infos => infos)
})
运行后,tmp文件夹中有一个文件,对应第一个select选项。
另外,文件名为bankPDF_0。
所以选项和索引不会在嵌套的噩梦部分中迭代,尽管它们在 reduce 回调中...
这是一些控制台输出:
nightmare:actions .evaluate() fn on the page +3ms
reduce it: option=1:accepted:1 index=0
reduce it: option=3:accepted:3 index=1
reduce it: option=4:accepted:4 index=2
nightmare:actions .wait() for select[name="acctRef"] element +3ms
nightmare:actions .select() select[name="acctRef"] +4ms
nightmare:actions .click() on input[type="submit"] +2ms
nightmare:actions .wait() for td a img element +4ms
nightmare:actions .click() on td a +7s
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
nightmare:actions .wait() for select[name="acctRef"] element +2s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +7s
nightmare:actions .click() on td a +2ms
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
nightmare:actions .wait() for select[name="acctRef"] element +1s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +6s
nightmare:actions .click() on td a +2ms
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
我在使用 reduce / promises 循环时做错了什么?
nightmare-inline-download 库中存在一个错误,当多次使用 .download()
时,会导致多个事件处理程序,强制下载的文件始终由附加的第一个处理程序处理。
我正在尝试加载 1 URL,然后 select select 菜单中的所有项目,以下载多个银行对帐单。
我正在按照指南 here 使用 reduce 进行循环。
部分代码:
const statementOptions = [
'1:accepted:1',
'3:accepted:3',
'4:accepted:4',
...
]
nightmare.goto('https://www.homesavings.com')
//login
.viewport(900, 800)
.wait('form[name="Login"]')
.insert('#userid', config.userid)
.insert('#password', config.password)
.click('#submit input')
// go to Online Statements page
.wait('a[data-app-code="Online Statements"]')
.goto('https://www.hslonline2.com/onlineserv/HB/OnlineStatements.cgi')
.evaluate(statementOptions => statementOptions, statementOptions)
.then(statementOptions => {
return statementOptions.reduce(function(acc, option, index) {
console.log(`reduce it: option=${option} index=${index}`)
return acc.then(function(results) {
return nightmare
// wait for the account select box to load..
.wait('select[name="acctRef"]')
.select('select[name="acctRef"]', option)
//.wait(1000)
.click('input[type="submit"]')
//.wait(2000)
.wait('td a img')
.click('td a')
.download(`${downloadDir}/bankPDF_${index}`)
.then(info => {
console.log(`after download. info -> ${JSON.stringify(info, null, 2)}`)
results.push(info)
return results
})
})
}, Promise.resolve([])).then(infos => infos)
})
运行后,tmp文件夹中有一个文件,对应第一个select选项。
另外,文件名为bankPDF_0。
所以选项和索引不会在嵌套的噩梦部分中迭代,尽管它们在 reduce 回调中...
这是一些控制台输出:
nightmare:actions .evaluate() fn on the page +3ms
reduce it: option=1:accepted:1 index=0
reduce it: option=3:accepted:3 index=1
reduce it: option=4:accepted:4 index=2
nightmare:actions .wait() for select[name="acctRef"] element +3ms
nightmare:actions .select() select[name="acctRef"] +4ms
nightmare:actions .click() on input[type="submit"] +2ms
nightmare:actions .wait() for td a img element +4ms
nightmare:actions .click() on td a +7s
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
nightmare:actions .wait() for select[name="acctRef"] element +2s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +7s
nightmare:actions .click() on td a +2ms
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
nightmare:actions .wait() for select[name="acctRef"] element +1s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +6s
nightmare:actions .click() on td a +2ms
after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}
我在使用 reduce / promises 循环时做错了什么?
nightmare-inline-download 库中存在一个错误,当多次使用 .download()
时,会导致多个事件处理程序,强制下载的文件始终由附加的第一个处理程序处理。