Selecting an element on Appium / Android with Python that has same Class and Same Index of another element on UIAutomatorViewer

nhrcpt Source

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.

androidpythonappiumappium-androiduiautomatorviewer

Answers

answered 2 days ago Chuk Ultima #1

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()

answered 2 days ago tcardoso #2

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/

answered 1 day ago nhrcpt #3

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.

comments powered by Disqus