I am trying to use Casperjs to get a list of links from a page, then open each of these links and add an object of a specific data type from these pages to the array.
The problem I am facing is the loop that runs on each of the items in the list.
First I get listOfLinks from the source page. This part works and uses length, I can check that this list is full.
However, using the this.each loop this.each , as shown below, none of the console statements ever appears, and casperjs skips this block.
Replacing this.each standard for the loop, execution only partially passes through the first link, since the "Create a new array in object for x.html" statement appears once, and then the code stops execution. Using IIFE does not change this.
Edit: in verbose debug mode:
Creating new array object for https://example.com [debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
For some reason, the URL that is passed to the thenOpen function changes to empty ...
I feel that there is something in Casperjs asynchronous nature that I donβt understand here, and would be grateful for pointing out a working example.
casper.then(function () { var date = Date.now(); console.log(date); var object = {}; object[date] = {}; // new object for date var listOfLinks = this.evaluate(function(){ console.log("getting links"); return document.getElementsByClassName('importantLink'); }); console.log(listOfLinks.length); this.each(listOfLinks, function(self, link) { var eachPageHref = link.href; console.log("Creating new array in object for " + eachPageHref); object[date][eachPageHref] = []; // array for page to store names self.thenOpen(eachPageHref, function () { var listOfItems = this.evaluate(function() { var items = []; // Perform DOM manipulation to get items return items; }); }); object[date][eachPageHref] = items; }); console.log(JSON.stringify(object)); });
javascript phantomjs casperjs
Laurence
source share