var links = []; var casper = require('casper').create(); function getLinks() { var links = document.querySelectorAll('h3.r a'); return Array.prototype.map.call(links, function(e) { return e.getAttribute('href'); }); } casper.start('http://google.fr/', function() { // Wait for the page to be loaded this.waitForSelector('form[action="/search"]'); }); casper.then(function() { // search for 'drake' from google form this.fill('form[action="/search"]', { q: 'drake' }, true); }); casper.then(function() { // aggregate results for the 'drake' search links = this.evaluate(getLinks); }); casper.run(function() { // echo results in some pretty fashion this.echo(links.length + ' links found:'); this.echo(' - ' + links.join('\n - ')).exit(); });