关于appium+python里self.driver.tap([(x,y)])无效的原因
今天写自动化,找不到元素点位,只能通过坐标去点击,于是使用tap去实现。
但是运行到tap的时候出错,提示找不到元素。去看了源码,发现并没有写错格式。
这里给个源码:
def tap(self, positions, duration=None):
"""Taps on an particular place with up to five fingers, holding for a
certain time
:Args:
- positions - an array of tuples representing the x/y coordinates of
the fingers to tap. Length can be up to five.
- duration - (optional) length of time to tap, in ms
:Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
"""
if len(positions) == 1:
action = TouchAction(self)
x = positions[0][0]
y = positions[0][1]
if duration:
action.long_press(x=x, y=y, duration=duration).release()
else:
action.tap(x=x, y=y)
action.perform()
else:
ma = MultiAction(self)
for position in positions:
x = position[0]
y = position[1]
action = TouchAction(self)
if duration:
action.long_press(x=x, y=y, duration=duration).release()
else:
action.press(x=x, y=y).release()
ma.add(action)
ma.perform()
return self
最后觉得可能是页面还没跳转就实现了tap,所有加了一个WebDriverWait去判断元素存在,最后成功实现。
因为之前一直用的click(),click是识别元素才进行点击的,然而坐标信息是一直存在的,所以tap会直接运行,前面务必加上延时或者判断!!!