Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to sendKeys in an EditText field

I am having issues on performing send keys in a particular EditText field. I tried a dirty xpath and id but still it does not enter into the text field. However I feel like it is having no issue finding the element. It doesn't click into the element however.

So what happens in the app is that when the user selects the page, it opens up a web view and the EditText field is in that WebView.

Here is the dirty xpath I got from the appium inspector:

/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.support.v4.view.ViewPager/android.view.ViewGroup/android.widget.EditText[1]

Here is the nice id:

@FindBy(id = "test_number")
public WebElement testNumberTextField;

I tried using find by xpath using the dirty xpath as well.

Here is my method where I try to enter the text. You can see by the commented code that I tried other things as well to get it working but still no success:

    public void enterTestNumber(){
    //List<WebElement> menu22 =drive.getDriver().findElements(By.className("android.widget.EditText"));
   // menu22.get(1).sendKeys(randomStringGeneratorMethods.randomAlphaNumericString(7));
    testPage.testNumberTextField.sendKeys(randomStringGeneratorMethods.randomAlphaNumericString(7));
    //js.executeScript("document.getElementById('"+testPage.testNumberTextField+").value='"+randomStringGeneratorMethods.randomAlphaNumericString(7)+"';");
    }

The random generator is just a random string generator which is this:

public String randomAlphaNumericString(int characterLength) {
    return RandomStringUtils.randomAlphanumeric(characterLength);
}

Does anyone know how to get this working so that it can type text into this EditText field?

like image 302
BruceyBandit Avatar asked May 10 '19 18:05

BruceyBandit


2 Answers

As the element is in the webview, you need to find the element html using chrome/firefox as the android ui automator would not be able to detect that element.

To get the html of the element, you need to connect your device with chrome and go to chrome://inspect, now operate in your app and open the webview, you would find your device in the chrome://inspect link as soon as the webview gets opened, you need to select your device from there and you would see the replica of the webview, from there you can inspect the element and find the html of the element and using that html you can get the xpath.

Now you have got the xpath, you just need to switch the driver to the webview from your code now and then you can enter the value in the field using sendKeys() method.

like image 122
Sameer Arora Avatar answered Nov 03 '22 17:11

Sameer Arora


I haven't tried this, but you could send adb shell command input text. after clicking the element.

with the Appium mobile command feature.

https://appium.io/docs/en/commands/mobile-command/

like image 3
wpj Avatar answered Nov 03 '22 17:11

wpj