0

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.

3 Answers 3

0

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.

0

If there is a delay between the calls to Clear and SendKeys, then this could explain your problem.

Then again, you may just have to do something like this https://stackoverflow.com/questions/3249583/selenium-webdriver-i-want-to-overwrite-value-in-field-instead-of-appending-to-i

I suspect that anonygoose gives a correct answer, but just like him, I cannot test to see for sure.

0

I had a similar problem that I solved using this:

driver.FindElement(By.Id(element)).SendKeys(Keys.Control + "a");
driver.FindElement(By.Id(element)).SendKeys(value);

It selects all the text in the first line, and then overwrites with the second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.