-1

I try to access successively different links on a page with Nightmare, but the second promise seems to be rejected (even when it shouldn't, and also with the".evaluate" instead of".exists").

Here is my function :

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()
    })
}
(full code : https://pastebin.com/wZxRvizk)

I've been there all day, I really don't understand why the promise is being rejected.

Output is :

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/' }
  • In your rejection handler, did you try to check the DOM subtree that the inner promise is trying to observe? Does it indeed have the element(s) you expect it to have? (Apparently you can't do it form the `.then` handler because it does not get executed.) – 9000 Apr 02 '19 at 16:51
  • Which catch() is triggered? Can you post the output? – Angrysheep Apr 02 '19 at 17:07
  • 2
    [Don't use `forEach`!](https://stackoverflow.com/q/37576685/1048572) – Bergi Apr 02 '19 at 18:13

1 Answers1

0

As Bergi said in comment, do not use forEach. use for..of for looping through list of urls.

for(let obj of innerTexts){
  await nightmare.goto(obj).wait(1100) // rest of your code
}
Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73