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

3

9

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.

Chagres answered 14/2, 2018 at 19:36 Comment(1)
You need to look into how to use webdriver to location the widget using the button with the word "Next". I dont have it handy, but that is what you need.Ealing
N
2

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()
Norward answered 23/2, 2018 at 13:22 Comment(0)
R
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/

Raby answered 23/2, 2018 at 17:48 Comment(0)
C
1

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.

Chagres answered 24/2, 2018 at 3:53 Comment(1)
Lookt at point 1: joecolantonio.com/2018/01/25/…Raby

© 2022 - 2024 — McMap. All rights reserved.