question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Appium cannot find element by ID, but can by XPATH

See original GitHub issue

The problem

I’ve been working on dual Appium/Selenium test suite for a web page and mobile app. When using Appium, it is unable to find the element by ID. If I change it to use XPATH, it works as expected, then fails to find the next element by ID. This may not sound like a problem, but other elements in the app are not easily distinguished by XPATH values.

This test suite worked previously with manual app install and the Appium Service running on Mac. I’ve since tried to automate the process to install the application and run Appium Service programmatically. I am wondering if the way I’ve set this up is causing it not to detect elements by ID.

I know for certain the ID is correct. It works for the Selenium + web page combination, and I’ve tried digging into the app with Appium Studio. It displays the ID correctly in the app, and copy pasting the ID into my code doesn’t change anything.

Environment

  • Appium version (or git revision) that exhibits the issue: All versions I’ve tried experience this (currently using 1.14.2)
  • Desktop OS/version used to run Appium: MacOS Catalina
  • Node.js version (unless using Appium.app|exe): 10.15.0
  • Npm or Yarn package manager: NPM
  • Mobile platform/version under test: Android 8.1
  • Real device or emulator/simulator: Real Device
  • Appium CLI or Appium.app|exe: Appium CLI

Link to Appium logs

Unfortunately, it doesn’t look like the AppiumService in Python creates logs. If it does, I’ll attach them when I find them or somebody tells me where they are

Code To Reproduce Issue [ Good To Have ]

Code below (redesign_tags and redesign_locators contain some sensitive info, but they work as intended in the Selenium flow):

class Driver:
    desired_capabilities = {}

    def __init__(self):
        # AppiumService was not called in previous version
        redesign_tags.appium_service = AppiumService()
        redesign_tags.appium_service.start()

        self.desired_capabilities[redesign_tags.platform_name_label] = redesign_tags.platform_device_name_android_label
        self.desired_capabilities[redesign_tags.no_reset_label] = redesign_tags.no_reset_value
        self.desired_capabilities[redesign_tags.platform_device_name_label] = redesign_tags.platform_device_name_galaxy_8
        self.desired_capabilities["app"] = "/Path/To/android-app.apk" # this line did not exist in the old version
        self.desired_capabilities[redesign_tags.app_package_label] = redesign_tags.app_package_value_dev
        self.desired_capabilities[redesign_tags.app_activity_label] = redesign_tags.app_activity_value
        self.instance = webdriver.Remote(redesign_tags.link_to_wd_hub, self.desired_capabilities)
        time.sleep(10)  # wait for application install
        print("Installed application")

def setupDevice():
    if redesign_tags.platform == "Android":
        print("Installing application...")
        driver = Driver()
    elif redesign_tags.platform == "Chrome":
        options = webdriver.ChromeOptions()
        options.add_argument('--start-maximized')
        options.add_argument('--unlimited-storage')
        driver = webdriver.Chrome(options=options)
        driver.get("localhost:8100")
        redesign_tags.webdriver = driver
        driver.set_window_position(0, 0)
        driver.maximize_window()
    else:
        print("Invalid platform specified")
        return False

    print("Registering device...")
    pairing = PairingPage(driver)
    # this element is not found by Appium
    pairing_credentials = pairing.get_element_by_id(redesign_locators.pairing_credentials_button, 25)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:31 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
KazuCocoacommented, Mar 8, 2020

Yes.

It depends on the app under test. You can find elements by io.appium.android.apis:id/text1 if the app under test had their customised resource id. Android’s general component could have android: prefixed resource id if nothing developers specified.

The behaviour is not Appium specific.

1reaction
mykola-mokhnachcommented, Jun 14, 2022

^ This could also be an issue with accessibility identifiers naming (app identifier prefix is missing). Then https://github.com/appium/appium/issues/15138#issuecomment-1127709588 could be used as a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot find an element with Xpath in Appium - Support
I have a problem - I cannot locate an element using Xpath, but I have no problems locating it using the ID locator....
Read more >
Appium cannot find by ID, can by Xpath - Stack Overflow
I am able to find elements by XPath but not by Id. I have tried in many ways as shown below // I...
Read more >
Cannot find an element with Xpath in Appium
I have a problem - I cannot locate an element using Xpath, but I have no problems locating it using the ID locator....
Read more >
Chapter-4: Appium Locator Finding Strategies - Kobiton
Finding an element using Class Name is generic and it does not guarantee to ... XPath is incredibly flexible as a fallback when...
Read more >
Appium Tip #18: How To Use XPath Locators Efficiently
Appium – based on Selenium foundation – provides excellent methods to locate elements by using an XPath function. And in fact, there are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found