appium python 用xpath和tap模拟点击

1、用xpath定位到元素
element = driver.find_element_by_xpath(xpath) #这里的xpath要换成元素的xpath信息

2、用.size和.location方法获取元素的大小和坐标

elem_size = element.size
elem_location = element.location


print出来elem_size和elem_location是这样的：
{‘width’: 285, ‘height’: 76}
{‘y’: 233.0, ‘x’: 28.0}

3、将size和location的数据处理成bounds

#根据size和location定位,返回tap中的参数
def coordinate(size, location):
_x_ = size.get('width')
_y_ = size.get('height')
x1 = location.get('x')
y1 = location.get('y')
x2 = x1 + _x_
y2 = y1 + _y_
return [(x1, y1), (x2, y2)]

4、最后调用tap方法模拟点击
bounds = coordinate(mesg.size, mesg.location)
driver.tap(bounds, 100)

全部的脚本示例：
#用xpath和tap模拟点击

def coordinate(size, location):
_x_ = size.get('width')
_y_ = size.get('height')
x1 = location.get('x')
y1 = location.get('y')
x2 = x1 + _x_
y2 = y1 + _y_
return [(x1, y1), (x2, y2)]

element = driver.find_element_by_xpath(xpath) #这里的xpath要换成元素的xpath信息
elem_size = element.size
elem_location = element.location
bounds = coordinate(elem_size, elem_location)
driver.tap(bounds, 100)


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客