segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

Evaluate not Working for me #1588

Closed OfficialCRUGG closed 2 years ago

OfficialCRUGG commented 4 years ago

Hey there. I am trying to do the following:

const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: false })
let url = "https://vivo.sx/5ec5b4cd00#k4zme0wz"
let source = await nightmare.goto(url)
                            .wait("source")
                            .evaluate(() => { document.querySelector("source").src; })
                            .end();
console.log(source);

however it just returns "null" all the time. I even tried setting "show" to true for the Nightmare Instance and setting the wait to 10000ms and then tried to open the DevTools manually inside the Electron Window and it works in there. But after the 10 Seconds the Window closes and it still returns null. How can I fix this?

Edit: Output when running with Debug Flag:

  nightmare queueing action "goto" for https://vivo.sx/5ec5b4cd00#k4zmswxw +17s
  nightmare queueing action "wait" +1ms
  nightmare queueing action "evaluate" +1ms
  nightmare running +1ms
  nightmare electron child process exited with code 0: success! +13s
ghost commented 4 years ago

await without async? very strange

OfficialCRUGG commented 4 years ago

Oh. In my code I have it in an async function. Just simplified it for the Issue and completely forgot about that.

ghost commented 4 years ago

.evaluate(() => { return document.querySelector("source").src; }) maybe?

stankoua commented 4 years ago

I tried to run the duckduckgo code snippet from the readme:

const nightmare = new Nightmare({
  show: true,
  openDevTools: {
    mode: 'right'
  }
})

nightmare
  .goto('https://duckduckgo.com')
  .type('#search_form_input_homepage', 'github nightmare')
  .click('#search_button_homepage')
  .wait('#r1-0 a.result__a')
  .evaluate(() => {
    console.log('this is inside the browser')
    return document.querySelector<HTMLLinkElement>('#r1-0 a.result__a')!.href
  })
  .wait(5000)
  .end()
  .then(console.log)
  .catch(error => {
    console.error('Search failed:', error)
  })

The script output is always undefined. Besides, the console.log in the evaluate function never print neither in the browser console (where it should according to the readme) or in my terminal. Evaluate function seems also broken for me.