gpt4 book ai didi

javascript - NightmareJS promise中的问题

转载 作者:行者123 更新时间:2023-12-03 13:24:15 25 4
gpt4 key购买 nike

我尝试使用Nightmare依次访问页面上的不同链接,但是第二个 promise 似乎被拒绝(即使不应该这样做,也可以使用“.evaluate”而不是“.exists”)。

这是我的功能:

async function getDatas(index) {
count = 0
await nightmare
.goto('https://xxx/login')
.wait('#username')
.insert('#username', login)
.insert('#password', password)
.click('#main-container div div div div div form button')
.wait('.card-title')
.run(() => {console.log("Waiting for main-container")})
.wait('#main-container')
.run(() => {console.log("Waiting 3s")})
.wait(3500)
.evaluate(function () {
return Array.from(document.querySelectorAll('td a')).map(element => element.href);
})
.then(async (innerTexts) => {
console.log("on charge liste projets");

await innerTexts.forEach(async function(obj){
console.log("On est dans le projet", obj)
await nightmare
.goto(obj)
.wait(1100)
.exists('.highlight')
// .evaluate(function() {
// return Array.from(document.querySelectorAll('a')).map(element => element.href);
// })
.then((re) => {
console.error(re)
console.log("It exists for", obj)
})


/* ********* Here is the triggered catch, but it shouldn't be ***** */
.catch(re => {
console.error("It doesn't exist for", re)
})
})
})
.then(() => {
nightmare.end()
})
.catch(error => {
nightmare.end()
})
}

(完整代码: https://pastebin.com/wZxRvizk)

我整天都去过那里,我真的不明白为什么 promise 被拒绝了。

输出为:
on charge liste projets
On est dans le projet https://sup-sub.cri.epita.fr/projects/20/
On est dans le projet https://sup-sub.cri.epita.fr/projects/21/
It doesn't exist for { Error: navigation error
at unserializeError (C:\temp\SupSub\linkedin-email-extractor\node_modules\nightmare\lib\ipc.js:162:13)
at EventEmitter.<anonymous> (C:\temp\SupSub\linkedin-email-extractor\node_modules\nightmare\lib\ipc.js:89:13)
at Object.onceWrapper (events.js:277:13)
at EventEmitter.emit (events.js:189:13)
at ChildProcess.<anonymous> (C:\temp\SupSub\linkedin-email-extractor\node_modules\nightmare\lib\ipc.js:49:10)
at ChildProcess.emit (events.js:189:13)
at emit (internal/child_process.js:820:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
code: -3,
details: 'ERR_ABORTED',
url: 'https://xxx/projects/20/' }

最佳答案

正如Bergi在评论中所说,请勿使用forEach。使用for..of循环浏览网址列表。

for(let obj of innerTexts){
await nightmare.goto(obj).wait(1100) // rest of your code
}

关于javascript - NightmareJS promise中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55479846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com