Issue using xpath with appium for native android apps

I am having an issue with using xpath in my scripts. When I use the Appium inspector to locate and element using this xpath “//android.widget.TextView[@text=‘My Messages’]” the inspector locates it with no problems. But when I try to use that xpath in my code like this

driver.findElement(By.xpath("//android.widget.TextView[@text='My Messages']")).click();

I get and error message that says

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 20.16 seconds For documentation on this error, please visit: Selenium Build info: version: ‘2.42.2’, revision: ‘6a6995d31c7c56c340d6f45a76976d43506cd6cc’, time: ‘2014-06-03 10:52:47’ System info: host: ‘C02P98XHG3QD’, ip: ‘127.0.0.1’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.9.5’, java.version: ‘1.7.0_79’ Session ID: 0701451b-c82d-7899-6030-099e74bc9fd4 Driver info: io.appium.java_client.android.AndroidDriver

I am not quite sure what I am doing wrong. Am I doing something wrong, or could this be an issue with Appium. I am currently using version 1.4.8

Try to remove android.widget. part from regular expression. And to search by text you need to use function text(), not attribute @text:
"//TextView[text()='My Messages']"

use text() function as suggested. Also if you could share the inspector hierarchy view that would be good.

Make it sure the element is in view while you are trying to locate it and perform any action on it

following both syntax gives error:- could not find element…

driver.findElementByXPath("//*[@class=‘ImageView’ and @content-desc=‘Navigation panel, button, double tap to open side panel’]").click();

driver.findElementByXPath("//*[@class=‘android.widget.ImageView’ and @content-desc=‘Navigation panel, button, double tap to open side panel’]").click();

Can you share the UI Automator screen shot for the element.

See if this helps:

//android.widget.ImageView[contains(@resource-id,’action_bar’) and contains(@content-desc,’Navigation panel’)]

driver.findElementByXPath("//android.widget.TextView[@text=‘Preference’]").click();