Alessandro Minoccheri Alessandro Minoccheri - 1 year ago 186
Javascript Question

get value of input in casperjs

Hi allm I would like to know how to get an input value with casperjs

This is my html element

<input type="hidden" name="key" value="newkey">

This is what I have tried:

view = 'users/registered';

casper.test.begin("Activating account", 5, function register(test){
casper.start(webroot + view, function(){

this.echo("Retrieving data from hidden input key", "COMMENT");

activationKey = this.evaluate(function() {
return __utils__.getFieldValue('key');

this.echo("Activating account with the key \"" + activationKey + "\"", "COMMENT");
window.location = webroot + activationKey;
}); {
this.echo('Account activated successfully', 'SUCCESS').exit();

casper.viewport(page.width, page.height);

In this case return

I have also tried:

activationKey = __utils__.getFieldValue('key');

but return me this error:

FAIL ReferenceError: Can't find variable: __utils__

Answer Source

Try using this :

this.getElementAttribute('input[type="hidden"][name="key"]', 'value');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download