I have a long script that executes several different links to do its job. I came across a script when PhantomJS ran out of resources due to the fact that the page objects were not garbage collected because I reused them. The following is a simple example. I close () the page object and create a new one at the end of each cycle of the cycle, but after I do this, Casper just freezes. What is the right way to do this?
var links = ['http://www.google.com', 'http://www.yahoo.com'];
var casper = require('casper').create({
logging: 'error',
pageSettings: {
webSecurityEnabled: false
}
});
casper.start('http://www.amazon.com', function () {
this.echo(this.getTitle());
});
casper.eachThen(links, function(item) {
var url = item.data;
this.open(url).then(function (){
this.echo(this.getTitle());
}).then(function (){
casper.page.close();
casper.page = require('webpage').create();
});
});
casper.run(function (){
this.echo('Done');
});
This will lead to the conclusion
Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Google
and then just hang. What am I missing?
source
share