mkreder mkreder - 3 years ago 178
Node.js Question

Nightmarejs code stuck with evaluate/then

Based on the example from the github page I wrote the following code:

// There is some stuff before this that loads the page and do a few clicks but it's not relative to the problem

var selector = 'ContentPlaceHolder1_dtlA_lnkB_0';
nightmare
.evaluate(function (selector) {
return document.querySelector(selector).innerText;
}, selector)
.then(function(text) {
console.log(text);
})


However, when I run the code it gets stuck with no output. The browser doesn't even appear.

DEBUG=nightmare node checkjobs.js
nightmare queuing process start +0ms
nightmare queueing action "goto" for http://xxxxxxxxx/Login.aspx +3ms
nightmare queueing action "type" +2ms
nightmare queueing action "type" +0ms
nightmare queueing action "click" +0ms
nightmare queueing action "wait" +0ms
nightmare queueing action "click" +0ms
nightmare queueing action "wait" +0ms
nightmare queueing action "click" +0ms
nightmare queueing action "evaluate" +0ms
nightmare running +1ms
nightmare running +16ms


However, if I comment the .then section. The rest of the code goes fine and the browser appears and does all the steps.

HTML looks like this:

<a id="CContentPlaceHolder1_dtlA_lnkB_0" class="LinkButonDark" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dtlA$ctl00$lnkB','')" style="color: #97c1e2; text-transform: capitalize;"><b>SOME TEXT I NEED TO CAPTURE</b></a>


What am I doing wrong?

Answer Source

In their example I always see .end() before .then().

Try this:

var selector = 'ContentPlaceHolder1_dtlA_lnkB_0';
nightmare
  .evaluate(function (selector) {
    return document.querySelector(selector).innerText;
   }, selector)
  .end()
  .then(function(text) {
    console.log(text);
  })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download