Python 无法使用monkeyrunner识别文本

Python 无法使用monkeyrunner识别文本,python,monkeyrunner,Python,Monkeyrunner,我有一个脚本,可以找到一个文本视图,获取它的坐标并点击它。为了单击,我必须滚动并找到文本视图。脚本如下: text = 'abc' self.device.drag((400,600), (300, 200), 0.01, 120) tv = self.vc.findViewWithText(text) if tv: (x, y) = tv.getXY() print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..."

我有一个脚本,可以找到一个文本视图,获取它的坐标并点击它。为了单击,我必须滚动并找到文本视图。脚本如下:

text = 'abc'
self.device.drag((400,600), (300, 200), 0.01, 120)
tv = self.vc.findViewWithText(text)
if tv:
    (x, y) = tv.getXY()
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y)
    tv.touch()
else:
        print "Text is not found" %text
它不会拖拉。虽然文本“abc”存在,但它打印“未找到文本”

我删除了drag()方法,并手动进行了拖动,效果很好(识别文本并单击)

有人知道我的drag()方法有什么问题吗


感谢

MonkeyRunner可以非常快地执行所有命令,因此在它开始查找视图之前,您需要在一段时间内添加睡眠。因此,您的代码将是这样的

text = 'abc'
self.device.drag((400,600), (300, 200), 0.01, 120)
MonkeyRunner.sleep(1)
tv = self.vc.findViewWithText(text)
if tv:
    (x, y) = tv.getXY()
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y)
    tv.touch()
else:
        print "Text is not found" %text

MonkeyRunner可以非常快地执行所有命令,因此在它开始查找视图之前,您必须在某些时候添加睡眠。因此,您的代码将是这样的

text = 'abc'
self.device.drag((400,600), (300, 200), 0.01, 120)
MonkeyRunner.sleep(1)
tv = self.vc.findViewWithText(text)
if tv:
    (x, y) = tv.getXY()
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y)
    tv.touch()
else:
        print "Text is not found" %text

AndroidViewClient.dump()
转储屏幕上当前显示的内容,因此如果必须滚动以使
TextView
可见,它将不会在第一次(隐式)转储中。 因此,您必须在滚动后再次转储:

text = 'abc'
self.device.drag((400,600), (300, 200), 0.01, 120)
MonkeyRunner.sleep(3)
self.vc.dump()
tv = self.vc.findViewWithText(text)
if tv:
    (x, y) = tv.getXY()
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y)
    tv.touch()
else:
        print "Text is not found" %text


还要考虑到所提到的关于睡眠的观点。

AndroidViewClient.dump()
转储屏幕上当前显示的内容,因此如果必须滚动以使
TextView
可见,则它不会在第一次(隐式)转储中。 因此,您必须在滚动后再次转储:

text = 'abc'
self.device.drag((400,600), (300, 200), 0.01, 120)
MonkeyRunner.sleep(3)
self.vc.dump()
tv = self.vc.findViewWithText(text)
if tv:
    (x, y) = tv.getXY()
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y)
    tv.touch()
else:
        print "Text is not found" %text

也要考虑到你提到的关于睡眠的问题