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?