Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  197] [ 5]  / answers: 1 / hits: 36928  / 12 Years ago, wed, november 14, 2012, 12:00:00

How can I login with CasperJS by submitting a form. I searched google and haven't found any good examples about it.


More From » phantomjs

 Answers
58

You will need to use Casper fill() function.



Below is an example which login to Facebook and print out your name after login. Note that you need to put in your username and password:



var casper = require('casper').create({   
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false, // use these settings
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on(page.error, function(msg, trace) {
this.echo(Page Error: + msg, ERROR);
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
console.log(page loaded);
this.test.assertExists('form#login_form', 'form is found');
this.fill('form#login_form', {
email: '**<put your email here>**',
pass: '**<put your password here>**'
}, true);
});

casper.thenEvaluate(function(){
console.log(Page Title + document.title);
console.log(Your name is + document.querySelector('.headerTinymanName').textContent );
});

casper.run();

[#81997] Tuesday, November 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
siena

Total Points: 199
Total Questions: 91
Total Answers: 91

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;