Skip to content

Instantly share code, notes, and snippets.

@catalint
Created May 10, 2014 17:25
Show Gist options
  • Save catalint/b53633ec318feefbc05f to your computer and use it in GitHub Desktop.
Save catalint/b53633ec318feefbc05f to your computer and use it in GitHub Desktop.
phantomjs example
var page = new WebPage(),
url = 'http://filipnet.ro',
stepIndex = 0;
page.onConsoleMessage = function (msg, line, source) {
console.log('console> ' + msg);
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onAlert = function (msg) {
console.log('alert!!> ' + msg);
};
page.onResourceRequested = function (request) {
// console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
page.onLoadFinished = function(status) {
console.log('.');
if (status === 'success') {
console.log('============================================');
console.log('Step "' + stepIndex + '"');
console.log('============================================');
page.injectJs('jquery-1.11.0.min.js',function(){
page.evaluate(function() {
var a = $('a[title=Contact]');
a.click();
console.log('Ma duc la pagina de contact...');
});
});
stepIndex++;
}
}
page.open(url);
// Step 2
function parseResults() {
console.log('aa results');
page.evaluate(function () {
console.log('Parsed results');
});
// If there was a 3rd step we could point to another function
// but we would have to reload the page for the callback to be called again
page.render('example.png');
phantom.exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment