I am using Nightmare.js (v2.3.3) to automate a part of my workflow where I have to access a website that updates our database. I have been able to get Nightmare working for basic things like .type
, .click
, and .screenshot
to validate that I am accessing these pages and inputting the information I intended.
Where I have been stuck, and the documentation seems lacking, is using .evaluate
to extract information from the page. In the documentation it has it as:
.evaluate(fn [,arg1,arg2,...])
var selector = 'h1';
var text = yield nightmare
.evaluate(function (selector) {
// now we're executing inside the browser scope.
return document.querySelector(selector).innerText;
}, selector); // <-- that's how you pass parameters from Node scope to browser scope
This is all well and good, but is it actually possible to go the opposite direction and pass information from browser scope to Node scope? What I would like to do is return all of the checkboxes on a page as an array, and then loop through them in the Nightmare script.
I also searched through many GitHub issues and StackOverflow questions to find an answer, and the problem seems to be that previous versions were built off of PhantomJS and v2+ is using Electron, so it is difficult to distinguish which answers actually still apply to the current version. Here is an answer that seemed to make sense to me, but it was in 2014 so I am thinking that most likely it was a PhantomJS version. For reference, this is the snippet that seemed to have an answer on how to transfer from browser to Node scope:
var p1=1,
p2 = 2;
nightmare
.evaluate( function(param1, param2){
//now we're executing inside the browser scope.
return param1 + param2;
}, function(result){
// now we're inside Node scope again
console.log( result);
}, p1, p2 // <-- that's how you pass parameters from Node scope to browser scope
) //end evaluate
.run();
But it does not seem that the current version of Nightmare supports this .evaluate(fn, cb, arg1, arg2,...)
format?
I would just like to know if this is possible before I drive myself crazy! Thanks for any and all help, please let me know if you need any additional information to help answer.