15

Selenium sendkeys with Chrome Driver drops character "2" and "4". Other characters are OK. When I use other browser (IE or FF), everything is OK.

code:

WebElement name = driver.findElement(localizator);
name.clear();
name.sendKeys("1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ");

result: input box is filled with

13567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ

Characters 2 and 4 are missing, other characters are filled correctly.

I use Windows 7 64bit, Chrome version 29.0.1547.57 m, ChromeDriver win32 (v2.2.215849.dyu) - the newest one.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Zuzana Rajova
  • 151
  • 1
  • 1
  • 3
  • 1
    Have you tried to send the String with many `sendKeys` calls? Is there any difference? – LaurentG Aug 28 '13 at 12:15
  • See also http://stackoverflow.com/questions/18013821/pythons-selenium-send-keys-with-chrome-driver-drops-characters – P.T. Feb 13 '15 at 18:51
  • I have this issue, but chrome itself will fail to accept a letter- today it's 's'. Seems to be chrome itself, and so Selenium cannot do anything. Capital 'S' is accepted. Very odd behaviour. Once I close this driver, it will start working again. – Daniel Williams Jan 31 '20 at 23:21

5 Answers5

9

Investigating you are from Czech Republic also, I am going to make wild assumption, that your keyboard is set up to Czech as default.

I also had some strange issues with sendKeys when my system had Czech keyboard as default one. Since I changed default to English, the problems dissapeared.

If this does not help, please provide info what is going to happen if you try this:

  name.sendKeys("2");
  name.sendKeys("22222222");
  name.sendKeys("4");
  name.sendKeys("44444444");
  name.sendKeys("424242");
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • Same problem with Russian keyboard, I have submitted issue: https://code.google.com/p/chromedriver/issues/detail?id=521 – Vladimir Sep 11 '13 at 09:42
7

It's an old question but still valid. I use Chrome Driver v2.53.

It looks like the keys are being send one by one to the browser (like a separate keyDown events). When it happens too fast then one of two results can be observable:

  • characters are shifted
  • characters are missing

My solution is as follows:

protected void sendKeys(final WebElement element, final String keys) {
    for (int i = 0; i < keys.length(); i++){
        element.sendKeys(Character.toString(keys.charAt(i)));
        waitUntil(attributeContains(element, "value", keys.substring(0, i)));
    }
}

It is reliable and works fast enough. What is more, when we want to clear the input field before sending the keys then the same event shift can occur, e.g:

element.clear();
element.sendKeys("abc");

It is possible that the clear operation will happen in one of four places:

  • before sending the letter "a"
  • before sending the letter "b"
  • before sending the letter "c"
  • after sending the letter "c"

I recommend to always check if the operation we've just executed has been accomplished successfully, e.g.: when we want to clear the input field it's a good practice to:

  1. check the value of the input field
  2. if the value is an empty string then return
  3. if the value is not an empty string then call clear() function and wait until the value will be equal to an empty string

It's a lot of operation to execute for a simple task. However, it will make the test more stable.

Mateusz Kleinert
  • 1,316
  • 11
  • 20
5

I had the same issue. I ended up calling sendkeys inside a loop until the correct value was inserted. Here's what I did:

WebElement name = driver.findElement(By.xpath(...));
this.sendkeys(name,"yourValue");

private void sendkeys(WebElement ele, String val) throws 
InterruptedException
{   ele.clear();
    while(true)
    {   ele.sendKeys(val);
        if(ele.getAttribute("value").equals(val))
            break;
        else
        {   ele.clear();
            Thread.currentThread();
            Thread.sleep(3000);
        }
    }

    Thread.currentThread();
    Thread.sleep(3000);
}

Hope this helps.

Faiz
  • 133
  • 1
  • 6
2

I also experienced this problem when connecting to a VM using VNC and then running the Selenium test that way.

The VNC was actually the one dropping characters. Once I moved to a direct connection to the VM using the VirtualBox console... it worked fine.

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
0

You can use java script to send text. See below code snippet.

public void sendTextToFieldWithJS (String str, WebElement element){
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].value='" + str + "';", element);
}
Bayram Binbir
  • 1,531
  • 11
  • 11