-1

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:

  • make sleep
  • make a slower input with sendKeys (so only 1 char then 300 milliseconds wait)

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+"')");
J Doe
  • 53
  • 7
  • 1
    Ensure the code works with a simple HTML form without Javascript handlers messing with with input. Test your automation against such a simplified form. – try-catch-finally Dec 31 '18 at 07:52
  • 1
    the code does work. like i said: the problem is not the code, its the auto-formation of the input field. why else it works correctly when entering a value < 1000? also this is a bigger project where i fill in forms all the time. it ALWAYS worked yet with exactly this code if there was no javascript handler messing with the input – J Doe Dec 31 '18 at 08:03
  • 1
    How long does it take to load the page? Do you wait long enough before entering values? I can imagine that the page does some initialization. If you do not wait long enough before entering values, the initial state might get reset _after_ you've typed keys. Though I think this is unlikely since one digit more seems to work just fine. Maybe you can _quickly_ enter three digits manually and reproduce it for manual input, though? – try-catch-finally Dec 31 '18 at 08:07
  • 1
    Well, like I said in my answer, you've to debug the handlers yourself or provide us with a [bare minimum example](https://stackoverflow.com/help/mcve) (maybe you can extract the relevant Javascript code into a minimum test page and narrow the cause down from there?). – try-catch-finally Dec 31 '18 at 08:09
  • Please add all other things you've _initially_ tried to your question. Use the "edit" link below you post. – try-catch-finally Dec 31 '18 at 09:01
  • 1
    ok, thank you for your efforts. the idea to edit the value directly via JavaScriptExecuter worked! I edited the Mainpost – J Doe Dec 31 '18 at 09:05

3 Answers3

0

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

0

The behavior you describe sounds like a bug to me. Assuming that it does not happen when you type manually it might be caused by the fact that Selenium "presses" the keys very quickly.

Waiting a little after each key press might help:

String[] digits = "200".split("");
for (String digit : digits) {
    inputElement.sendKeys(digit);
    Thread.sleep(250);
}

Without knowing the implementation of these handlers it's not possible to narrow down the cause.

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
0

You can try this,

int met = 4200;
driver.findElement(By.id("metal")).sendKeys("" + met);

and also this,

String met = "4200";
driver.findElement(By.id("metal")).sendKeys(String.valueOf(met));

or

driver.findElement(By.id("metal")).sendKeys(Keys.NUMPAD4, Keys.NUMPAD2, Keys.NUMPAD0, Keys.NUMPAD0);

and also using JavaScript,

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('metal').value='4200';");

Give it a try.

  • You just provide different ways of converting the number to a string (shall make absolutely no difference) or entering the same char sequence (makes probably no difference). Assuming entering the value via Javascript works _in this case_, it could probably cause other problems since the required event handlers are not triggered. – try-catch-finally Dec 31 '18 at 08:54
  • @try-catch-finally Yes, because he needs to enter that number. So he should try all the possible methods. Sometimes the first method works with these situations and Javascript also should work. –  Dec 31 '18 at 08:56