Is there a way to get and set the focused element? (Native App)

Hi there !

Is there a way to get and set the focused element (without clicking or sending keys to it)?
I’m currently testing an app where clicking or sending keys to a TextField open a new window, so I’m using driver.getKeyboard().sendKeys() to write in the focused text field, and driver.pressKeyCode(AndroidKeyCode.KEYCODE_TAB) to navigate between text fields, but I need to do a specific case for each view, which is very tedious.

I’ve tried action.moveTo(textFieldToFocus).perform(), textFieldToFocus.sendKeys("") to set the focus, but it doesn’t work.
I’ve also tried to get the focused element by doing textFieldToFocus.equals(driver.switchTo().activeElement()), but it throws an exception “Method not yet implemented”.

Does someone have a solution? :slight_smile:

I’ve found a way to get the focused element :

driver.findElementByXPath("//*[@focused=‘true’]"),

but still no method to set the focused element…

1 Like

I have found that in the case of some Android NATIVE_APP context modal dialogs, that this can be overcome by doing the Click Element twice to the same element in repitition. The first time presumably sets the focus to the element, and the second actually Clicks the Element. I am using Robot Framework.

hello,have you figure out it?