Skip to content

Instantly share code, notes, and snippets.

@gwing33
Created October 12, 2012 23:14
Show Gist options
  • Save gwing33/3882163 to your computer and use it in GitHub Desktop.
Save gwing33/3882163 to your computer and use it in GitHub Desktop.
casperjs script to change google search location
casperjs google_search_location.js pizza 'phoenix, az'
var casper = require('casper').create({
clientScripts: [ 'includes/jquery.min.js' ],
pageSettings: { userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'}
});
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
var search_arg = casper.cli.args[0];
var location_arg = casper.cli.args[1];
casper.on('remote.message', function(msg) {
this.echo('Browser: ' + msg);
});
casper.start('http://google.com/', function() {
this.fill('form[action="/search"]', { q: search_arg }, true);
});
casper.then(function() {
console.log("Changing Location");
this.evaluate(function(loc) {
var my_loc = $('.tbos');
console.log('My Location - ' + my_loc.text());
console.log('New Destination - ' + loc);
var form = document.getElementById('change_location_form');
var input = document.getElementById('lc-input');
input.value = loc;
google.x(form,function(){ google.loc.submit(); });
setTimeout( function() {
var loc = $('.tbos');
console.log('Current Location - ' + loc.text());
var links = $('h3.r a');
var my_links = Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
console.log(my_links.length + ' links found:');
console.log(' - ' + my_links.join('\n - '));
}, 5000);
}, { loc: location_arg });
});
casper.run(function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment