2

I tried to scrape web page generated by AJAX. On the web page inside <div class='main'> are several <a class='classLinks'> elements and after click on one of them is being content generated in the same <div class='main'> and <a class='classLinks'> elements are replaced with one <a class='back'> back button.

My task is grab content generated after click on all <a class='classLinks'> elements inside <div class='main'> and write it to console log. I put all <a class='classLinks'> to the array of selectors but problem is I don't know how to run sequence ".click, .wait, .evaluate., .click" inside for cycle.

Steps are:

goto http://example.com
click on subpage
get all "a" elements
content = ""
for (i=0; i < a.lenght; i++)
{
  click on a[i]
  content = content + div.innerHTML;
  click on "a.back"
}
console.log content;

My code looks:

var content
nightmare
  .goto('http://example.com')
  .wait('.firstpage')
  .click('a.subpage')
  .wait('.main')


  .evaluate(function(){
    var links = document.querySelectorAll('a.classLinks');
         for (i = 0; i < links.length; ++i) {
        links[i].click();
        // now i need to wait for <div class='main'>
        .wait('.main')
        // and do evaluate again
        .evaluate(function(){ 
            //content is glogal variable
            content = content + document.querySelector('div.main').innerHTML;
            // and click on 'back' link becaus all a.classLinks disappeared
            document.querySelector('a.back').click();
         })
         
        
    })
    return content;
  .end()
  .then(function(content) {   
    console.log(content);
  })

Please how to process several evaluations inside for cycle in nightmare?

JEROME
  • 139
  • 1
  • 8
  • did you find out how to do this? – Tiago Jun 30 '17 at 15:55
  • You can do this via storing HTMLElements list inside page and then using index to run several evaluations. Please note that each evaluation should be in the then() clause of previous. Refer my answer: (Screenshot)[https://stackoverflow.com/questions/44580348/nightmare-js-screenshot-buffer-length-0/44708987#44708987] – devilpreet Jul 01 '17 at 06:17

0 Answers0