0

I've created an Appium test in Python which recreates signing into a mobile application.

I was able to successfully run my test using the ACCESSIBILITY_ID element, but I know this is not something all developers add even though it should be done. Using Appium Inspector, I found that each element has an ID field that contains a native identifier for what that element is. When switching my script to use ID, it fails and states no element could be located on the page using the given search parameters.

I am using WebDriver for this Python test. Here is a brief snippet of what my test looks like:

# Appium server connection
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# Wait for the app to load to give time for elements
time.sleep(2)

try:
    username_field = driver.find_element(By.ID, "username")
    username_field.click()
    print("Clicked on the username input field.")
except Exception as e:
    print(f"Error while finding or clicking on the username input field: {e}")

This works:

username_field = driver.find_element(By.ACCESSIBILITY_ID, "Username input field")

This doesn't:

username_field = driver.find_element(By.ID, "username")

The error message is:

Traceback (most recent call last):
  File "/Users/user/Desktop/Appium/test.py", line 24, in <module>
    username_field = driver.find_element(By.ID, "username")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/appium/webdriver/webdriver.py", line 409, in find_element
    return self.execute(RemoteCommand.FIND_ELEMENT, {'using': by, 'value': value})['value']
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/appium/webdriver/errorhandler.py", line 122, in check_response
    raise exception_class(msg=message, stacktrace=format_stacktrace(stacktrace))
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
NoSuchElementError: An element could not be located on the page using the given search parameters.
    at AndroidUiautomator2Driver.findElOrEls (/Users/user/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/find.js:75:11)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at AndroidUiautomator2Driver.findElOrElsWithProcessing (/opt/homebrew/Cellar/appium/2.0.0/libexec/lib/node_modules/appium/node_modules/@appium/base-driver/lib/basedriver/commands/find.ts:60:12)
    at AndroidUiautomator2Driver.findElement (/opt/homebrew/Cellar/appium/2.0.0/libexec/lib/node_modules/appium/node_modules/@appium/base-driver/lib/basedriver/commands/find.ts:75:12)

I did some research on the Appium and Selenium docs and found two things:

  1. Accessibility_ID uses content-desc attribute. When I checked for this attribute value in my app using Inspector, it does indeed contain the same value that I am providing the variable username_field.
  2. ID uses resource-id for Android. When I checked for this attribute value in my app using Inspector, I also see that it exists and matches the value I pass along to the variable username_field.

Continuous testing result in the same problem, where ACCESSIBILITY_ID works and ID does not.

I've attached a screenshot of the Appium Inspector window when I select the Username input field. As can be seen, both the ACCESSIBILITY_ID field and ID fields exist along with attributes that match the docs from Appium.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 27 '23 at 21:47
  • Can you please share the XML Layout as identified for the UI using Appium Inspector tool or UIAutomator tool? – Bipin Kumar Chaurasia Aug 01 '23 at 11:20

0 Answers0