无法使用monkeyn标识

2024-10-04 01:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()方法有什么问题吗。在

谢谢


Tags: 方法text文本self脚本视图ifdevice
2条回答

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

或者

^{pr2}$

还要考虑到NRP提到的关于睡眠的问题。在

相关问题 更多 >