casperjs fillXPath not accepting variable as selector: Unable to find field element in form

  casperjs, javascript

I try to fill a field via the casperjs fillXPath method.

When I provide the selector directly, the field is found and filled.
When I provide the selector in a variable, the fill fails with error : Unable to find field element in form.

What I am trying to do

casper.then(function() {
    var field = '//input[@id="dhcp_start[0]"]';
    var value = "192.168.104.1";
    this.echo("test var equality : " + (field == '//input[@id="dhcp_start[0]"]')); // true
    this.echo("value before first fill : " + this.getFormValues('form')["dhcp_start[0]"]); // empty

    this.fillXPath('form', { '//input[@id="dhcp_start[0]"]': value }, false);
    this.echo("value after first fill : " + this.getFormValues('form')["dhcp_start[0]"]); // 192.168.104.1

    this.fillXPath('form', { field: value }, false); // CasperError: Errors encountered while filling form: Unable to find field element in form: FieldNotFound: setField: Invalid field ; only HTMLElement is supported
    this.echo("value after second fill : " + this.getFormValues('form')["dhcp_start[0]"]); 
}

Output

test var equality : true
value before first fill :
value after first fill : 192.168.104.1
CasperError: Errors encountered while filling form: Unable to find field element in form: FieldNotFound: setField: Invalid field ; only HTMLElement is supported

Extract from the form

<div class="itfDHCPRangeInput">
    <input type="text" name="dhcp_start[0]" value="" size="15" maxlength="15" id="dhcp_start[0]">
</div>                                      
<div class="itfDHCPRangeInput">
    <input type="text" name="dhcp_end[0]" value="" size="15" maxlength="15" id="dhcp_end[0]">
</div>

Does anyone have a clue as to how to solve this problem?
Thanks in advance !

Source: Ask Javascript Questions

LEAVE A COMMENT