I am testing an app and on most of the screens I see that there are elements that have the same class "android.widget.TextView" with same index number "0". All other properties also same, only exceptions are the "text" and "bound".
I have "Skip", "Next" and "Skip Next 3" as texts on the screen which has the same attribute other than the text and bounds Attribute. I need to know how I can point appium to click on the desired item .. say I want to click on "Next", how can I do this. I am using Python for the scripting.
Finding elements using the ID is, by far the simplest technique. Each element has a unique ID assigned to it that helps in identifying and interacting with it. Appium has a Native element identifier for Android and iOS. resource-id is used as an element identifier for Android and name is used for iOS.
resource-id for android; name for iOS. Use the UI Automator API, in particular the UiSelector class to locate elements. In Appium you send the Java code, as a string, to the server, which executes it in the application's environment, returning the element or elements.
When using Appium findElement by XPath, the program analyzes the XML structure of the app and locates a particular element relative to other elements. In fact, originally this strategy was developed to navigate XML data in order to locate unique UI elements. Remember that XPath selectors are not cross-platform.
To find the DOM element use "UIAutomateviewer" to find DOM element for Android application. Connect your Phone or Emulator with Appium desktop client. Start Inspect Session. Start session by specifying capabilities for device - deviceName, platformName, automationName, appPackage, appActivity.
You can search for all matching web elements with the same class name, which will return you a list of the matching elements. Then you loop over the found elements and you compare their text, e.g :
for element in webdriver.find_elements_by_class_name('android.widget.TextView'):
if element.text == "Next":
element.click()
Doing as Chuk Ultima said will work, but if you have lots of TextView it may take a while.
You can use:
((AndroidDriver<MobileElement>)driver).findElementByAndroidUIAutomator("new UiSelector().text(\"Next\")");
See more usages in http://www.automationtestinghub.com/uiselector-android/
Well, In my case I finally solved the problem using traversing through nodes like I created xpath to get the Text (time value) from a particular location like this:
Time = self.driver.find_element_by_xpath('//android.widget.LinearLayout[@index=2]/android.view.ViewGroup[@index=0]/android.view.ViewGroup[@index=0]/android.view.ViewGroup[@index=0]/android.view.ViewGroup[@index=3]/android.view.ViewGroup[@index=0]/android.widget.TextView[@index=0]').get_attribute('text')
I understand this is a tedious process, but for the time being this is the solution for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With