Soft keyboard not present, cannot hide keyboard -

2019-02-17 06:29发布

问题:

I am getting following exception:

 org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (Original error: Soft keyboard not present, cannot hide keyboard) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 368 milliseconds

I am using driver.hideKeyboard() to hide soft input keyboard that is open on the screen.
How to ensure that keyboard is open before hiding it? OR any other workaround?

回答1:

Use adb command to check whether keyboard has popped up or not

adb shell dumpsys input_method | grep mInputShown 
Output : mShowRequested=true mShowExplicitlyRequested=false mShowForced=false mInputShown=true

if mInputShown=true then yes software keyboard has popped up. Then use driver.pressKeyCode(AndroidKeyCode.BACK);

One way to do using java is

Process p = Runtime.getRuntime().exec("adb shell dumpsys input_method | grep mInputShown");
        BufferedReader   in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String outputText = "";

           while ((outputText = in.readLine()) != null) {

               if(!outputText.trim().equals("")){
                        String keyboardProperties[]=outputText.split(" ");
                        String keyValue[]=keyboardProperties[keyboardProperties.length-1].split("=");

                        String softkeyboardpresenseValue=keyValue[keyValue.length-1];
                        if(softkeyboardpresenseValue.equalsIgnoreCase("false")){
                                isKeyboardPresent=false;
                        }else{
                                isKeyboardPresent=true;
                        }
               }
           }
           in.close();

PS: Please do not use driver.navigate().back() as its behavior may not be same on all devices.



回答2:

I also get this error, i correct it by using the following code in the setUp method :

capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);

You can check answers here : Keyboard in Android physical device isn’t always hidden while using Appium