Appium/Android Help getting find element to work

I am new to using Selenium with Appium for mobile testing. We have used selenium web driver to automate our website and looking to move towards automation of the mobile app.

I am hoping I could reach out for a few quick pointers. I am not able to select this element to then send text to begin the log-in process and I have been researching for hours to find a possible video help etc.

Below is a screen shot from Appium, I am trying to select the email field and I cant see to use the ID or the xpath: https://screencast.com/t/KokZ2tCgjLG4

I am looking to find the element and then sendkeys to the field so I can log into the app.

Any help or direction is greatly appreciated.

Thank you,

Here is my code:
def setUp(self):
desired_caps = {}
desired_caps[‘platformName’] = ‘Android’
desired_caps[‘platformVersion’] = ‘8.0’
desired_caps[‘deviceName’] = ‘Pixel’
desired_caps[‘app’] = PATH(
‘/Users/stevenrutherford/desktop/Android/foodlogiq-x86.apk’
)
desired_caps[‘appPackage’] = ‘com.foodlogiq.connect’
desired_caps[‘appActivity’] = ‘.MainActivity’

    self.driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)


def test_login(self):
    time.sleep(15)

    email = self.driver.find_element_by_xpath("//*[@contentDescription='EMAIL ']") #I have try multiple options here
    email.click()

And I can’t seem to get Appium to find the element:
[HTTP] --> POST /wd/hub/session/a56058ad-94e9-4a69-a554-ad7bc3d2b6b7/element {“using”:“xpath”,“sessionId”:“a56058ad-94e9-4a69-a554-ad7bc3d2b6b7”,“value”:"//[@contentDescription='EMAIL ']"}
[MJSONWP] Calling AppiumDriver.findElement() with args: [“xpath”,"//
[@contentDescription='EMAIL ']",“a56058ad-94e9-4a69-a554-ad7bc3d2b6b7”]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 0 ms for condition
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//[@contentDescription='EMAIL ']",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//
[@contentDescription='EMAIL ']",“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 '//*[@contentDescription='EMAIL ‘]’ using ‘XPATH’ with the contextId: ‘’ multiple: false

It sits at this point without clicking the element or moving forward to even attempt to enter the email address.

you are using wrong xpath.

try this one-
//*[contains(@content-desc,‘EMAIL’)]

@Shubham_Agarwal updated my code and I believe it is working now:

self.driver.find_element_by_xpath(’//*[contains(@content-desc,“Email”)]’)

However, I am trying to send keys to this text field and once it find the element is doesn’t appear to move forward for the rest of the code, any idea why that would be?

[HTTP] --> POST /wd/hub/session/c7aa718f-7c9b-4747-a3ca-3d6191162386/element {“using”:“xpath”,“sessionId”:“c7aa718f-7c9b-4747-a3ca-3d6191162386”,“value”:"//[contains(@content-desc,“Email”)]"}
[MJSONWP] Calling AppiumDriver.findElement() with args: [“xpath”,"//
[contains(@content-desc,“Email”)]",“c7aa718f-7c9b-4747-a3ca-3d6191162386”]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 0 ms for condition
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//[contains(@content-desc,“Email”)]",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//
[contains(@content-desc,“Email”)]",“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 ‘//*[contains(@content-desc,“Email”)]’ using ‘XPATH’ with the contextId: ‘’ multiple: false

Thank you for your help on this I greatly appreciated it.

Here is the console output:
Error
Traceback (most recent call last):
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”, line 331, in run
testMethod()
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/AndroidTest.py”, line 34, in test_login
element = self.driver.find_element_by_xpath(’//*[contains(@content-desc,“Email”)]’).send_keys(“Test”)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 385, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 955, in find_element
‘value’: value})[‘value’]
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 310, in execute
response = self.command_executor.execute(driver_command, params)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py”, line 466, in execute
return self._request(command_info[0], url, body=data)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py”, line 528, in _request
resp = opener.open(request, timeout=self._timeout)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 431, in open
response = self._open(req, data)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 449, in _open
‘_open’, req)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 409, in _call_chain
result = func(*args)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 1200, in do_open
r = h.getresponse(buffering=True)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 1132, in getresponse
response.begin()
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 453, in begin
version, status, reason = self._read_status()
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 417, in _read_status
raise BadStatusLine(line)
BadStatusLine: ’

It should be noted this is almost an Android hybrid app.

Post a screen grab with the ui automator viewer

@Wai_Chu


@Steven_Rutherford not the screen grab from the appium inspector. If you have Android Studio, the UI Automator Viewer may be in the folder sdk\tools\bin

@Steven_Rutherford
Nevermind the UI Automator Viewer. The inspector screen grab should suffice. Have you tried this?

email_field = self.driver.find_element_by_android_uiautomator(‘new UiSelector().className(“android.widget.EditText”).description(“Email”)’)
email_field.click()
email_field.set_value(‘E-mail goes here’)

Thank you, that got me further than the xpath method, however, I am getting the following:
Error
Traceback (most recent call last):
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”, line 331, in run
testMethod()
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/AndroidTest.py”, line 33, in test_login
email_field = self.driver.find_element_by_android_uiautomator(‘new UiSelector().className(“android.widget.EditText”).description(“Email”)’)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/appium/webdriver/webdriver.py”, line 156, in find_element_by_android_uiautomator
return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 955, in find_element
‘value’: value})[‘value’]
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 312, in execute
self.error_handler.check_response(response)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

If I adjust it to find elements I get the following. I attempted the click from the index 0 but it also failed.
Error
Traceback (most recent call last):
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”, line 331, in run
testMethod()
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/AndroidTest.py”, line 34, in test_login
email_field.click()
AttributeError: ‘list’ object has no attribute ‘click’

The description “Email” is listed for both the .EditText and the view.View in that hierarchy.

@Steven_Rutherford

you shouldn’t need to use elements as there appears to only be one element on that screen with that class and description combination. Maybe you should attempt to find another element on the screen before the email field - to ensure that the proper screen is even up before it searches for the field. Maybe the terms of use button?

@Wai_Chu

I am not able to locate that element either, do I need to do something to switch to the webview to see these elements?

@Steven_Rutherford

Strange. If you’re able to get to the login screen in the Inspector with those desired capabilities, you should also be able to get to the login screen by running your py script that has the same parameters. I’m assuming you’re testing on an actual device - so, when you run your script, does the login screen ever show up in the phone’s display?

@Wai_Chu
I am testing on a virtual device powered through Android Studio. When I run the script it brings up the app on screen and displays the log-in screen. Just not able to locate the elements on the screen.

@Wai_Chu

So I got this working to the point it will select my element… (issue was with the emulator I was running, went from android 8.0 back to 6.0 and it works)

    email_field = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Email ")')
    email_field.click()
    email_field.set_value('[email protected]') 

Error
Traceback (most recent call last):
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”, line 331, in run
testMethod()
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/AndroidTest.py”, line 35, in test_login
email_field.set_value(‘[email protected]’)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/appium/webdriver/webelement.py”, line 167, in set_value
self._execute(Command.SET_IMMEDIATE_VALUE, data)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”, line 628, in _execute
return self._parent.execute(command, params)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 312, in execute
self.error_handler.check_response(response)
File “/Users/stevenrutherford/PycharmProjects/FoodLogiQ_Android/venv/lib/python2.7/site-packages/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

@Steven_Rutherford

There’s got to be something up with your emulator. What if you take out the email_field.click()?

@Wai_Chu Thank you for all of your help! Without the .click() it is able to set the value. I was able to get the whole log-in process working!

@Steven_Rutherford

no prob. As an alternative, you can also try:

email_field.send_keys(‘[email protected]’)
try:
self.driver.hide_keyboard()
except:
pass

I always include the hide keyboard try/except code after inputs because the keyboard sometimes hides whatever element I’m looking for afterward.

You should really try to use Py3 though. 2.7 is going to be deprecated.
Have fun learning Appium with Python. It can be frustrating, as you experienced, and rewarding.

Great… happy to see that you found the solution :slight_smile: