Selenium sendkeys drops character with Chrome Driver

后端 未结 4 1512
时光取名叫无心
时光取名叫无心 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:05

    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");
    
    0 讨论(0)
  • 2021-01-04 07:17

    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.

    0 讨论(0)
  • 2021-01-04 07:21

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题