I want to fill a input field on a website with selenium webdriver. the input field looks like this:
<input type="text" pattern="[0-9,.]*" class="checkThousandSeparator hideNumberSpin" name="metal" tabindex="1" id="metal" value="0" onblur="updateVariables();" onkeyup="checkRessourceByType('metal'); updateVariables();" onkeypress="return submitOnEnter(event);">
So I never had a problem at filling an input field before. but this one auto-formats the input, so if you enter 24424 it automatically makes 24.424 out of it.
If I use the Webdriver now to enter a value, I can enter it (for example 4200) and the input field makes 4.200 out of it. but when I submit the form which belongs to the input field, it changes the value to 0. the crazy thing is if I enter a value below 1000 and the input field doesn't need to format it, I can submit the right number. Anyone knows why?
what I already tried:
But both didn't help. here is my code -
//doesn't work, form will submit a "0"
int met = 4200;
driver.findElement(By.id("metal")).sendKeys(Integer.toString(met));
//works, form submits the right value
int met = 200;
driver.findElement(By.id("metal")).sendKeys(Integer.toString(met));
There is no error or anything
Edit: I forgot to say: The form only fails to submit the right value, if I enter the value with selenium Webdriver. if I enter the
value manually, the form always submits the right value, even if it's > 1000.
Edit2 Solution: Ok. Instead of using the sendKeys()-Method, it is required to set the value via JavaScriptExecuter.
//This works:
int met = 550000;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('metal').setAttribute('value', '"+met+"')");
You Can remove the pattern attribute and the class attribute using selenium. Then you can try to enter the value you want.
Try out it