.clear() is not working anymore, what is the alternative

Hello, recently with the latest Appium we cannot anymore use the .clear() for clearing the text field. What can we use as an alternative since the below code does not work as well

element.click()
element.send_keys(Keys.CONTROL + 'a')
element.send_keys(Keys.DELETE)

Tried to use Keys.chord() but it’s not working too, but maybe because it’s mobile?

any more bit of info: ios / android / ?

For both :confused: Once we used Appium-Python-Client 2.11.0 and 2.10.2 it stopped working properly.
.clear() does not work for our script anymore.

Try a look How to clear a pre-filled text field (Android)
https://stackoverflow.com/questions/22679960/appium-clear-a-field

Weird. For now it doesn’t support continuous line of code?
Failed:
self.el_username_field().clear().send_keys(username)

Pass:

    self.el_username_field().clear()
    self.el_username_field().send_keys(username)
    self.el_password_field().clear()
    self.el_password_field().send_keys(password)

Gives an error AttributeError: 'NoneType' object has no attribute 'send_keys'
It was working before. Hmmm.

try logic:

  1. tap on input element
  2. search input element AGAIN
  3. now try clear it
  • with Android in 99% it will complete successfully

in some cases we need to use different logic. e.g. with one field of our app I have below.
see with android tried 3 different ways. one works better exactly with on this screen.
EACH command search for element first and then makes clear or sendKeys.

    @iOSXCUITFindBy(id = "generated_AmountTextField")
    @AndroidFindBy(className = "android.widget.EditText")
    private WebElement amountInput;

        if (isIOS) {
            Assert.assertTrue(clearInput(amountInput), createAssertionLog("FAILED clear 'amountInput'"));
            Assert.assertTrue(setValue(amountInput, amount), createAssertionLog("FAILED"));
        } else {
            Assert.assertTrue(tap(amountInput), createAssertionLog("FAILED"));
            Assert.assertTrue(clearInput(amountInput), createAssertionLog("FAILED clear 'amountInput'"));
            // double for sure
            sleep(250);
            for (int i = 0; i < 4; i++)
                pressKey_Android(AndroidKey.DEL);
            sleep(250);
            //*
            // does not work well here
            // Assert.assertTrue(setValue(amountInput, amount), createAssertionLog("FAILED"));
            //*/
            // driver.executeScript("mobile: type", ImmutableMap.of("text", amount));
            sendTextToKeyboard(amount);
            // hideKeyboard();
        }

    @Step("Press Key Android")
    public boolean pressKey_Android(AndroidKey Key) {
        Logger.log();
            try {
                ((AndroidDriver) driver).pressKey(new KeyEvent(Key));
                return true;
            } catch (Exception ex) {
                return false;
            }
        return false;
    }

    @Step("Send text to keyboard {text}")
    protected boolean sendTextToKeyboard(String text) {
        Logger.log("text: '" + text + "'");
        try {
            new Actions(driver).sendKeys(text).perform();
            // deprecated
            // driver.getKeyboard().sendKeys(text);
            return true;
        } catch (Exception e) {
            Logger.log();
            e.printStackTrace();
            return false;
        }

    }

Lemme take a look at those. Thanks Aleksi and Giuma.