Locating Elements on Android App

I am able to launch my app but after I get to a pin screen, I am having hard time locating element. The screenshot from uiautomator looks like the one in the attached file. I have tried all these options and they result in NoSuchElementException

self.driver.find_element_by_id
self.driver.find_element_by_class_name
self.driver.find_element_by_android_uiautomator

My desired capabilities looks like this

    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '7.1'
    desired_caps['deviceName'] = 'OnePlus3T'
    desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/base.apk'))
    desired_caps['appPackage'] = 'com.xxxxxxx.xxxxxx'
    desired_caps['appActivity'] = 'com.xxxxxx.xxxxxx.MainActivity'
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

Also this is a native app running on real hardware device. I see the same issue when I use emulator. I have been struggling with this problem for a week not. I want to get some help from community before giving up.

Give some wait in executing cmds and try using xpath

Are you using the complete name for the element, e.g. com.:id/pin_input

What error do you see on failure?

Yes. here is my script

def test_activation(self):
sleep(3)
with open(‘activation_code.txt’, ‘r’) as f: activation_code=f.read()
self.driver.find_element_by_id(‘com.x.x:id/code_input’).click()
self.driver.find_element_by_id(‘com.x.x:id/code_input’).send_keys(activation_code)
self.driver.find_element_by_id(‘com.x.x:id/submit_button’).click()
self.driver.implicitly_wait(10)

    # FAILS                                                                                                                                               
    sleep(10)
    self.driver.find_element_by_id('com.x.x:id/pin_input').click()
    self.driver.find_element_by_id('com.x.x:id/pin_input').send_keys('1234')

Here is the ERROR

======================================================================
ERROR: test_activation (main.SmokeTest)

Traceback (most recent call last):
File “smoke.py”, line 39, in test_activation
self.driver.find_element_by_id(‘com.x.x:id/pin_input’).click()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 269, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 752, in find_element
‘value’: value})[‘value’]
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 236, 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 192, 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.


Ran 1 test in 50.793s

FAILED (errors=1)

Could you please give me some direction on how to extract xpath of an element in android. The uiautomator does not display xpath. Is there something similar to firebug on Android?

I wil update u jst provide me a classname@ @jesse_online

Sure. The class name is android.widget.EditText shown in the screenshot.

xpath("//classname[@locator=‘locator value’]")
@jesse_online
Example
xpath("//android.widget.EditText[@text=‘xyz’]")

Thanks for your help. I am still having issues. The script just waits and does nothing. This is what I see on the Appium Server

[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: [“xpath”,"//android.widget.EditText[@text=“PIN”]",“6daa8b2b-8312-48bf-86e6-fdec6f9c2bfc”]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 10000 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.widget.EditText[@text=“PIN”]",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.widget.EditText[@text=“PIN”]",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding ‘//android.widget.EditText[@text=“PIN”]’ using ‘XPATH’ with the contextId: ‘’ multiple: false

======================================================================
ERROR: test_activation (main.SmokeTest)

Traceback (most recent call last):
File “smoke.py”, line 39, in test_activation
self.driver.find_element_by_xpath(’//android.widget.EditText[@text=“PIN”]’).click()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 752, in find_element
‘value’: value})[‘value’]
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 234, in execute
response = self.command_executor.execute(driver_command, params)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py”, line 401, in execute
return self._request(command_info[0], url, body=data)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py”, line 471, in _request
resp = opener.open(request, timeout=self._timeout)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”, line 466, in open
response = self._open(req, data)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”, line 484, in _open
‘_open’, req)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”, line 444, in _call_chain
result = func(*args)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”, line 1282, in http_open
return self.do_open(http.client.HTTPConnection, req)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”, line 1257, in do_open
r = h.getresponse()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”, line 1197, in getresponse
response.begin()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”, line 297, in begin
version, status, reason = self._read_status()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”, line 266, in _read_status
raise RemoteDisconnected(“Remote end closed connection without”
http.client.RemoteDisconnected: Remote end closed connection without response

@jesse_online Try passing this statement-- xpath ("//android.widget.EditText[@text=‘PIN’]"))

@jesse_online Did it work.

@s10v10s It still does not work.

Traceback (most recent call last):
File “smoke.py”, line 39, in test_activation
self.driver.find_element_by_xpath("//android.widget.EditText[@text=‘PIN’]").click()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”, line 752, in find_element
‘value’: value})[‘value’]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remot

HERE IS THE SCRIPT
sleep(10)
# import pdb; pdb.set_trace()
self.driver.find_element_by_xpath("//android.widget.EditText[@text=‘PIN’]").click()
self.driver.find_element_by_xpath("//android.widget.EditText[@text=‘PIN’]").send_keys(’****’

@jesse_online Problem is u r using remote, use android driver in desired capabilities in place of remote try tht

@s10v10s

Do you mean changing this line

self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)
To
self.driver = webdriver.Android(‘http://localhost:4723/wd/hub’, desired_caps)

I am getting this error:
AttributeError: module ‘appium.webdriver’ has no attribute ‘Android’

Here is my desired capability

    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '7.1'
    desired_caps['deviceName'] = 'OnePlus3T'
    #desired_caps['deviceName'] = 'Nexus_5X_API_24'                                                                                                                   
    desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/base.apk'))
    desired_caps['appPackage'] = 'com.*.*'
    desired_caps['appActivity'] = 'com.*.*.MainActivity'
    self.driver = webdriver.Android('http://localhost:4723/wd/hub', desired_caps)

I have not specified appwaitactivity. Just wondering if it has anything to do with the issue.

Cn u use 0.0.0.0 in place of local host and start appium m server with same @jesse_online

Jst to confirm what is the this webdriver what r u initializing it as globally

@s10v10s Attached is my full script. I am importing webdriver from appium module. smoke_mask.txt (1.8 KB)

from appium import webdriver

@jesse_online
Add this in capabilities*
desired_caps[‘newCommandTimeout’] = 5000

Try this wait
def wait_for_dismiss
ignore { wait(5) { dismiss } }
end

def dismiss
“your code”
end

Actually, I am more of a java person, not having deep knowledge with python so could try out in script. :confused:

Is ur application getting launched ???

Try to use Android driver instead of Remote driver.

Try to refine yr xpath . Use only text property , don’t use layout name.