I’m trying to get an element by id and this id is set programmatically.
Example:
In my Android views I set an ID like this view.setId(R.id.myId) and on Appium I can’t find this element by this ID. This ID also doesn’t show in Appium Inspector Tool
Does this id shown in Android inspector? Guess no. Cause in android you cant set ids dynamically it should be some predefined resource. Anyway check with android inspector first that is in sdk tools.
if you cant see them in UIAutomatorView you cant do anything. as workaround, when we need to set something dynamic, we use content description. Which is visible and we can find it:
el.getAttribute("content-desc")
// or
@AndroidFindBy(uiAutomator = "new UiSelector().description(\"your_description\")")
private MobileElement someLabel;
// or
MobileElement el = driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().description(\"your_description\")"));
If the attribute is not visible in the page source then there is probably no way to access it using automation framework because it is simply not exposed to the accessibility. Try your luck with Espresso
Espresso indeed has some means of retrieving the created id, but we are migrating to Appium because we test both Android and iOS in a single project. By the way, in iOS the ids created using ‘setId’ show in its inspector (accessibility-id \ name) and are accessible by Appium. So we have this Appium-Android behavior that is hindering our project. Like @tuliopereirazup said, do you think it’s possible to contribute somehow with Appium to enable retrieving such ids? Or is it a contribution to Android \ UIAutomator2 instead?
Yes, it would be rather a contribution to the Android Accessibility and/or UiAutomator framework. Regarding Espresso: Appium has a driver for it as well: https://github.com/appium/appium-espresso-driver