0

I have a listview with several list items in it. Each item is a relative layout that has an image button and a nested linear layout which has a textview with unique text. I need to be able to find an item in the list based on the text in the textview, and then click the image button next to it. Based on the UiAutomator API it looks like the best way to do this would be to use the fromParent method which allows you to access siblings of a parent view. I have no trouble scrolling and finding the textview, but I have been unsuccessful creating a UiSelector for its parent view or the image button.

self.web_driver.find_element_by_android_uiautomator('new UiScrollable(
new UiSelector().resourceId("' + self.id_prefix + 'list_view")).scrollIntoView(
new UiSelector().text("' + identifier + '")) ;')

self.web_driver.find_element_by_android_uiautomator('new UiSelector().fromParent(
new UiSelector().text("' + identifier + '"))')

The first call works flawlessly, and I know the selector to find the view works. The problem is using that UiSelector to get a UiSelector for the parent view.

UiSelector API, fromParent method

Stack Trace

Traceback (most recent call last):
  File "/.../test_suites/data_management.py", line 46, in test_delete
    self.delete(valid)
  File "/.../android_data_management.py", line 45, in delete
    'list_view"))')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/appium/webdriver/webdriver.py", line 110, in find_element_by_android_uiautomator
    return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/appium/webdriver/errorhandler.py", line 24, in check_response
    super(MobileErrorHandler, self).check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

Copy of the xml layout. Red- textview I use to find the list item. Blue- the list item, green- the image button I want to click

Anthony Porter
  • 152
  • 1
  • 9

1 Answers1

0

I have faced this issue and my solution does not use UI automation script, though give it a try if it works for you.

  1. Create a POJO for the data inside relative layout.
  2. Get the List view element.
  3. Get each relative view element inside the list view
  4. Initialize and assign the element/text to the POJO object and add object to a HashSet/Hashmap
  5. Identify the desired relative layout based on the text.
  6. get the ImageButton element from the object and click on it.
pr4bh4sh
  • 654
  • 3
  • 12
  • 19