Search code examples
javaseleniumxpathappium

Appium NOT locating element when java variable is used in xpath


I'm trying to locate elements dynamically usign the xpath. However, when I use variable in the xpath, elements are NOT located. However, if I use hardcoded value, elements are located properly.

What am I missing here?

Below xpath locates the elements perfectly:

driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(@value, 'hp')]"));

whereas, below xpath doesn't locate the elements:

driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(@value, '" + device + "')]"));

Please note that , there are multiple elements matching the above xpath.

I even tried below code but of no use:

driver.findElements(By.XPath(String.Format("//XCUIElementTypeStaticText[contains(@value, '{0}')]", device)));

Any help would be appreciated.


Solution

  • The issue was with the case mismatch in the value returned by variable. i.e; device variable was returning 'hP' instead of 'hp'.

    Corrected the code and it works fine now.