Unable to tap button on Android Webview

EDIT: Managed to find the issue, see my edit at the bottom

Hello there, I have a hybrid app, when I access the webview I can see in UIAutomator inspector the element with the following hierarchy:

/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ListView/android.widget.Button

I can click it just fine via UIAutomator inspector, but haven’t had any success at all doing it via the script (python) even by using the same xpath that supposedly UIAutomator used to click it. I also tried changing the driver context to webview with chromedriver but it seems in there half of the locators don’t work: Message: invalid argument: invalid locator

The element is clickable and has the following attributes:

|index|0|
|class|android.widget.Button|
|text|getmoreapps|
|bounds|[49,377][1393,503]

However any attempts to search by xpath, class name or text result in: An element could not be located on the page

ie.

getmoreapps = driver.find_element_by_xpath(‘//android.widget.ListView/android.widget.Button’)
getmoreapps.click()

This has blocked me for hours and I don’t really want to do it via coordinates, as that won’t work for other screen sizes. I also tried using find element by android uiautomator, to no avail

Any ideas? I also have full access to the webview and the bundle, in case there is a way to create unique identifiers for these buttons, but I don’t know how to do that.

Thank you very much

EDIT: I have found a workaround that works, I noticed on the UI inspector that if I opened it with the webview already on the phone it won’t let me select individual elements, just the whole WebView class, but if I navigated back to the native app and then opened the WebView with UI inspector it will actually show all elements in there so I have applied this to the script (going back to native and then opening webview) and it works. But it’s still not an ideal fix, any ideas what causes this?

many ways to try:

  1. switch to webview
  2. use simpler finder e.g. by className ‘android.widget.Button’

Thanks for the suggestions, however isn’t point nr 1 only for those hybrid apps that start with webview? In my app I do a bunch of stuff in the native context before going into webview

I had three buttons that presented this issue and I’ve been able to fix two of them via the workaround that I mention in the edit on my post. There is another button that seems tricky though but will keep trying.
Thanks!

you may switch to web and back to native on any app screen and even in any action e.g. only for tap some button in webView and switch to native right after tap like:

    @Step("Tap 'Add account' button")
    public AccountTypePage tapAddAccountButton() {
        Logger.log();
        switchToWebContext();
        Assert.assertTrue(tap(addAccountButton), createAssertionLog("FAILED"));
        switchToNativeContext();
        return new AccountTypePage(page);
    }