python uiautomator TypeError: 'AutomatorDeviceObject' object is not callable

python uiautomator TypeError: 'AutomatorDeviceObject' object is not callable

以上是带有属性的 uiautomator 对象查看器屏幕截图。我正在使用通过 pip install uiautomator 安装的 python 包 uiautomator。我正在尝试通过 UI automator 对象访问三点设置按钮。我尝试使用 https://github.com/xiaocong/uiautomator#handler

中列出的文档

这是代码和错误:

import uiautomator as uia
d = uia.device() #below all are my attempts
>>> d(resourceId="com.android.camera2:id/three_dots").clickable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle").clickable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="id/mode_options_toggle")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle").clickable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle").clickable()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle", className="com.android.camera2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle", className="com.android.camera2").clcik()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle", className="com.android.camera2").click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/mode_options_toggle", className="com.android.camera2").click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(package="com.android.camera2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/progress_overlay")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/progress_overlay")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(resourceId="com.android.camera2:id/progress_overlay")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className="android.view.View")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className="android.view.View").child(className="android.widget.LinearLayout", resourceId="com.android.camera2:id/mode_options_toggle")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className="android.view.View").child(className="android.widget.LinearLayout", resourceId="com.android.camera2:id/mode_options_toggle").click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(text="").info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d.info
{u'contentDescription': u'', u'checked': False, u'clickable': False, u'scrollable': False, u'text': u'', u'packageName': u'com.android.camera2', u'selected': False, u'enabled': True, u'bounds': {u'top': 0, u'left': 0, u'right': 1440, u'bottom': 2560}, u'className': u'android.widget.FrameLayout', u'focusable': False, u'focused': False, u'checkable': False, u'resourceName': None, u'longClickable': False, u'visibleBounds': {u'top': 0, u'left': 0, u'right': 1440, u'bottom': 2392}, u'childCount': 2}
>>> d(packageName=u'com.android.camera2).info
  File "<stdin>", line 1
    d(packageName=u'com.android.camera2).info
                                            ^
SyntaxError: EOL while scanning string literal
>>> d(packageName=u'com.android.camera2').info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className=u'com.android.camera2').info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className=u'com.android.camera2').info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className=u'').info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className=u'android.widget.FrameLayout').info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> d(className="android.widget.FrameLayout").info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'AutomatorDeviceObject' object is not callable
>>> 

经过所有尝试,我仍然无法访问任何 UI 对象。请让我知道我在这里遗漏了什么?

不是您问题的解决方案,而是替代方案:使用 AndroidViewClient/culebra 您可以 运行

culebra -uG -o camera-options.py

获得 window 后,您可以单击 选项

立即查看结果

同时,将生成包含重现此操作的代码的脚本

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2017  Diego Torres Milano
Created on 2017-11-24 by Culebra v13.5.2
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_s = 5
_v = '--verbose' in sys.argv


kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)
#vc.dump(window='-1') # FIXME: seems not needed

vc.dump(window=-1)
vc.findViewWithContentDescriptionOrRaise(u'''Options''').touch()

变化:

import uiautomator as uia
d = uia.device()

收件人:

import uiautomator as uia
d = uia.device

这应该可以解决您的问题。

发生这种情况是因为您在变量内调用函数 device()。所以"d"不是函数,而是用函数接收一个值

当我们使用括号调用函数时 Python 理解我们正在调用它并将此结果用于您的变量 "d"。这就是您面临 "object is not callable" 的原因,因为没有对象作为参数传递给 "device()"。

当我们不使用括号时我们不调用该函数,我们引用它并在我们调用它时传递参数。

希望对您有所帮助!