Selenium sendkeys drops character with Chrome Driver

后端 未结 4 1514
时光取名叫无心
时光取名叫无心 2021-01-04 06:53

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:



        
4条回答
  •  既然无缘
    2021-01-04 07:29

    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.

提交回复
热议问题