Skip to content

Instantly share code, notes, and snippets.

@johntitus
Created February 23, 2015 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntitus/1cbdaa5f0f3a9afbe35f to your computer and use it in GitHub Desktop.
Save johntitus/1cbdaa5f0f3a9afbe35f to your computer and use it in GitHub Desktop.
Multiple evaluates in PhantomJS work fine
var page = require('webpage').create();
var click = function( selector, callback ){
page.evaluate( function( selector ) {
var element = document.querySelector(selector);
var event = document.createEvent('MouseEvent');
event.initEvent('click', true, true);
element.dispatchEvent(event);
return true;
}, selector );
callback();
};
var wait = function( time, callback ){
setTimeout( callback, time );
};
var type = function( selector, text, callback ){
page.evaluate( function( selector ){
document.querySelector( selector ).focus();
return true;
}, selector);
for (var i = 0, len = text.length; i < len; i++){
page.sendEvent( 'keypress', text[i], null, null, 0 );
}
callback();
}
page.open('https://github.com', function(status) {
console.log("opened github? ", status);
click('a[href="/login"]', function(){
wait( 2000, function(){
type( 'input[name=login]', 'myusername', function(){
type( 'input[name=password]', 'mypassword', function(){
page.render('out.png');
phantom.exit();
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment