javascript - How to do nested looping over many pages in CasperJS -
i don't have clue start this. need casperjs run through 15 different pages, each page runs through needs data 150 different locations need set cookie values. each location, need check data 5 different dates.
any 1 of these seems pretty straight forward, trying 3 happen confusing me.
i tried set way:
for(iterate through urls){ for(iterate through locations){ for(iterate through dates){ phantom.addcookie({ // cookie data here based on location , date }); casper.start(url) .then(function(){ // stuff here }) .run(); } } }
essentially loop through everything, load page based on last link, @ last location, on last date. every other location gets skipped. there easier way this? perhaps better, there way tell javascript loop wait casper finish doing needs before jumping next loop iteration?
i'm happy provide more details if needed. tried simplify process best can without cutting out needed info.
that's pretty it. 2 things out for:
casper.start()
,casper.run()
should called once per script. can usecasper.thenopen()
open different urls.keep in mind
casper.then*()
,casper.wait*()
functions asynchronous step functions , scheduled execution after current step. since javascript has function level scope, need "fix" iteration variables each iteration otherwise last url. (more information)
example code:
casper.start(); // deliberately empty (var url in urls) { (var location in locations) { (var date in dates) { (function(url, location, date){ casper.then(function(){ phantom.addcookie({ // cookie data here based on location , date }); }).thenopen(url) .then(function(){ // stuff here }); })(url, location, date); } } } casper.run(); // start scheduled steps
if use array.prototype.foreach
instead of for-loop, can safely skip use of iife fix variables.
i'm not sure, may need first open page add cookie domain. may possible phantomjs accepts cookie when domain cookie open.
Comments
Post a Comment