As per my requirement I have to enter credit card number which is 4111 1111 1111 1111.

I am trying with sendKeys(), it is working in all browsers except IE browser. When it is trying to enter the number in IE browser its only entering 1 or 2 digits. Not sure why it is happening with IE browser only.

Please help me!

  • 3
    Call sendKeys for each digit. – Florent B. Apr 25 '18 at 9:44
  • As @FlorentB suggests, the IE interface is slow and in some ways buggy. Write a sendkeys wrapper that sends the keystrokes one-a-a-time if the browser is IE. – Bill Hileman Apr 25 '18 at 13:53
  • Obviously, the solution is drop support for IE. =) – corsiKa Apr 26 '18 at 1:32
  • @FlorentB. You should make an answer =) – corsiKa Apr 26 '18 at 1:33

Use JavaScriptExecutor instead of the SendKeys:

String cardNum= "4111111111111111";
WebElement inputField= driver.findElement(By.id("cardnum"));

JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[1].value = arguments[0]; ", cardNum, inputField); 
  • 4
    By writing the value directly, you are no longer simulating a user input. The element could be disabled, not interactable and it doesn't emit the events expected the page like keydown, keyup, input, change, focus). Thus this should never be used in test. – Florent B. Apr 25 '18 at 11:51
  • 2
    @FlorentB. I guess it depends on what you're testing. I think your point is well articulated, but I also think never is a strong word. – corsiKa Apr 26 '18 at 1:33
  • 1
    @corsiKa, a test is supposed to validate a behavior, not just work, especially with an e2e test. So in this case never is just the right word. That is if you want to write a useful test. Don't get me wrong, I'm not saying that injecting some JS in the page should never be done. I'm saying it should never be done as presented in this solution since it doesn't assert the interactibility nor emit any event. – Florent B. Apr 26 '18 at 12:43

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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