Appium : Clear a field

后端 未结 26 2943
野趣味
野趣味 2020-12-16 16:03

I have my login screen in app now. Each time the app is launched in screen the mobile number is pre filled with the older text.

I just want to know I have tried:

相关标签:
26条回答
  • 2020-12-16 16:50

    I faced the same issue.

    Tried to click, clear and send keys, it worked.

    See this code

    driver.findElement(By.name("address").click(); 
    driver.findElement(By.name("address").clear();
    driver.findElement(By.name("address").sendKeys("something");`
    
    0 讨论(0)
  • 2020-12-16 16:50
    List<WebElement> x=driver.findElements(By.className("enter widget details")); //You can get widget details via android UIautomator
    driver.findElementById("enter widget details").click(); //You can get widget details via android UIautomator
    x.get(index).clear();
    x.get(index).clear();
    

    Please note this worked for me but you may have to maneuver your code as per your requirements, thanks.

    0 讨论(0)
  • 2020-12-16 16:51

    It is definitely not efficient, could be improved and there's probably a better way... But, using adb's shell input key event codes I simply called "dpad right" to move the cursor all the way to the right. Once there, send the key code "DEL" to start deleting all the way back... So... two for-loops. This was mainly used for short texts:

    public void cleatTextFully(WebElement element) {
        int stringLength = element.getText().length();
    
        for (int i = 0; i < stringLength; i++) {
            mDriver.sendKeyEvent(22); // "KEYCODE_DPAD_RIGHT"
        }
    
        for (int i = 0; i < stringLength; i++) {
            mDriver.sendKeyEvent(67); // "KEYCODE_DEL"
        }
    }
    

    mDriver is the AppiumDriver instance. Hope this helps some what.

    0 讨论(0)
  • 2020-12-16 16:52

    Is working for me:

    action.longPress(WebElement).perform();
    WebElement.clear();
    
    0 讨论(0)
  • 2020-12-16 16:53

    To avoid recent iOS 8.x errors I use the following (where by is an instance of By representing your textfield/textview):

        int stringLength = driver.findElement(by).getText().length();
        if (stringLength > 0) {
            driver.findElement(by).clear();
        }
    

    If you try and call clear() against an already clear textfield I was tending to get errors in appium.

    0 讨论(0)
  • 2020-12-16 16:55

    I had trouble with this too. A main issue I found was that in order to delete the area by pressing the delete key was that it needed to tap at the end of the line. This works for me:

    public void clearTextField(WebElement element) {
        double x = element.getLocation().getX() + element.getSize().width - 5;
        double y = element.getLocation().getY() + ((double) element.getSize().height / 3);
        preciseTap(x, y, 0.1, 1);
        while (!element.getText().isEmpty()) {
            pressDeleteKey();
        }
    }
    
    public void preciseTap(double x, double y, double duration, int touchCount) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, Double> tapObject = new HashMap<String, Double>();
        tapObject.put("x", x);
        tapObject.put("y", y);
        tapObject.put("touchCount", (double)touchCount);
        tapObject.put("duration", duration);
        js.executeScript("mobile: tap", tapObject);
    }
    
    public void pressDeleteKey() {
        HashMap swipeObject = new HashMap();
        swipeObject.put("keycode", 67);
        ((JavascriptExecutor) driver).executeScript("mobile: keyevent", swipeObject);
    }
    

    It is a lot slower than just clearing it all out but I have not figured out how to do this yet. Would be ideal to double tap or tap and hold until everything is selected.

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