Search code examples
python-2.7androidviewclient

AndroidViewClient: How to check if "Id/Text/Image" exist before touch it?


How can I check if exist an ID/Text before touch it?

I was trying with this:

# class=android.widget.ImageView
com_evernote___id_close = vc.findViewByIdOrRaise("com.evernote:id/close")
if not com_evernote___id_close:
   vc.sleep(1)
else:
   com_evernote___id_close.touch()

After LogIn on Evernote. It sometimes shows some help info. So if it exits I want to close if not the script continue executing.

And when it does not exist shows this error:

  File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.0.10-py2.7.egg/com/dtmilano/android/viewclient.py", line 3352, in findViewByIdOrRaise
raise ViewNotFoundException("ID", viewId, root)
com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='com.evernote:id/close' in tree with root=ROOT

Solution

  • If you don't want to raise an Exception if the View is not found then use ViewClient.findViewById() instead of ViewClient.findViewByIdOrRaise(). Then check if the returned values is not None. That simple!