Trouble deleting NaN in input field



  • I'm using Selenium to fill in a web form. The fields there have their default values, usually it's 0. If I issue the command

    Driver.chrome.FindElement(By.Id("ID_of_my_element")).Clear();
    

    the value changes from 0 to NaN. The following input

    Driver.chrome.FindElement(By.Id("ID_of_my_element")).SendKeys("12345");
    

    results in NaN12345. The desired result should be 12345.

    This the HTML of the said field:

    <input class="form-control two-decimals text-box single-line" 
    data-val="true" data-val-number="The field myField must be a number." 
    data-val-required="The myField field is required." id="myId" 
    name="myName" style="max-width:100%; width:500px;" type="text" value="0.00">
    

    Is there a way for me to fill these fields since my usual .Clear() and then SendKeys() route isn't giving me the expected result.



  • Looks like you may want to use the javascript executor to get around the validation modifying the value inside the field between the Clear and SendKeys actions.

    IWebElement field = driver.FindElement(By.Id("myId"));
    ((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].setAttribute('value', '12345')", field);
    

    Syntax not guaranteed on this, as I don't have a C# environment set up.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2