Created
November 3, 2013 11:08
-
-
Save cb372/7289063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require('casper').create(); | |
casper.cli.drop("cli"); | |
casper.cli.drop("casper-path"); | |
if (casper.cli.args.length === 0 && Object.keys(casper.cli.options).length === 0) { | |
casper.echo('Usage: phantomjs download-java.js <name of file to download>'); | |
casper.echo('Example: phantomjs download-java.js jdk-7u45-linux-x64.rpm'); | |
casper.exit(1); | |
} | |
var filename = casper.cli.get(0); | |
casper.echo('Will download file: ' + filename); | |
// Echo calls to console.log | |
casper.on('remote.message', function(msg) { | |
this.echo('Remote log: ' + msg); | |
}); | |
// Spoof our UA, just in case Oracle is checking for bots | |
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'); | |
casper.start('http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html', function() { | |
this.echo('Opened page [' + this.getTitle() + ']'); | |
}); | |
casper.then(function() { | |
// Click the first radio button | |
this.click('input[type="radio"]'); | |
this.echo('Clicked the Accept License Agreement button'); | |
}); | |
// Find the first link containing the given text, and return its ID | |
function findDownloadLinkId(linkText) { | |
var links = document.querySelectorAll('a'); | |
console.log('Found ' + links.length + ' links on page'); | |
for (var i=0; i<links.length; i++) { | |
if (links[i].innerText.indexOf(linkText) > -1) { | |
console.log('Found the link. Id = ' + links[i].id); | |
return links[i].id; | |
} | |
} | |
} | |
casper.then(function() { | |
var linkId = this.evaluate(findDownloadLinkId, filename); | |
// ID is invalid (because it contains a dot?), | |
// but luckily Oracle's crazy HTML has name attributes as well as IDs | |
this.click('a[name="' + linkId + '"]'); | |
this.echo('Clicked the Download link'); | |
// Can't actually download the file :( | |
// Waiting for this feature to be merged: | |
// https://github.com/ariya/phantomjs/issues/10052 | |
}); | |
casper.run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment