Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 575
nhrcpt Avatar asked Feb 14 '18 19:02

nhrcpt


People also ask

How will you identify elements in Appium?

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.

How do I find an element using resource-ID in Appium?

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.

What is the use of find elements method in Appium test script?

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.

How do I find DOM element or XPath in mobile app?

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.


3 Answers

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()
like image 91
Chuk Ultima Avatar answered Sep 30 '22 19:09

Chuk Ultima


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/

like image 28
tcardoso Avatar answered Sep 30 '22 19:09

tcardoso


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.

like image 33
nhrcpt Avatar answered Sep 30 '22 18:09

nhrcpt