之前曾经写过一部分关于点击屏幕某座标的分享,今天特用代码来分享;
一)梳理
1.tap()是webdriver里面定义的方法,是两个参数,第一个是positions,是list类型,最多五个点(是不是可以代表五个手指),duration是持续时间,单位毫秒
driver.tap([(x,y),(a,b),(c,d),(e,f),(g,h)],duration)
以下是官方文档资料
实际调用的是TouchAction类下面的触摸tap()、短按press()、长按long_press()
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
2.使用adb命令
import os
adb1 = ‘adb shell input tap X Y’
os.system(adb1)
3.TouchAction类下面的触摸tap()
tap(self, element=None, x=None, y=None, count=1) 触摸:模拟手指触摸屏
以下是官方文档资料
def tap(self, element=None, x=None, y=None, count=1):
"""Perform a tap action on the element
:Args:
- element - the element to tap
- x - (optional) x coordinate to tap, relative to the top left corner of the element.
- y - (optional) y coordinate. If y is used, x must also be set, and vice versa
:Usage:
"""
opts = self._get_opts(element, x, y)
opts['count'] = count
self._add_action('tap', opts)
return self
4.TouchAction类下面的短按press()
press(self, el=None, x=None, y=None) 短按:模拟手指按住一个元素,或者坐标
以下是官方文档资料
def press(self, el=None, x=None, y=None):
"""Begin a chain with a press down action at a particular element or point
"""
self._add_action('press', self._get_opts(el, x, y))
return self
二)实践
如上图,要对已经复制到剪贴板的内容做粘贴操作(就是对 粘贴 做点击操作),代码如下:
1.driver执行tap()
driver.tap([(120, 1700), (125, 1750)], 500)
2.adb命令执行tap操作
import os
adb1 = 'adb shell input tap 120 1700'
os.system(adb1)
3.TouchAction类下面的触摸tap()
from appium.webdriver.common.touch_action import TouchAction
TouchAction(self.driver).tap(x=120, y=1700).perform()
4.TouchAction类下面的短按press()
from appium.webdriver.common.touch_action import TouchAction
# Press 要release 释放手指
TouchAction(self.driver).press(x=120, y=1700).release().perform()
交流技术 欢迎+QQ 153132336 zy