python+appium【第六章-TouchAction与输入操作】
Appium的TouchAction操作
在上一章我们学习了如何获取屏幕分辨率,待会儿我们可以用到触摸操作上
我们先打开手机上的触摸坐标,这样我们每次点击的时候都会显示当前的坐标
- 先找到开发者选项
- 打开指针位置
- scroll 从一个元素移动到另一个元素
scroll(self,origin_el,destination_el,duration=None):模拟从元素origin_el滚动至元素destination_
scroll(self,origin_el,destination_el,duration=None): 模拟从元素 origin_el 滚动至元素 destination_el
drag_and_drop(self,origin_el,destination_el):将元素origin_el拖到目标元素destination_el
拿vivox20举例:
import time from appium import webdriver from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.228.102:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # 上滑操作 scroll 从一个元素移到另一个元素 web_element_e1 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="用户和账号"]') web_element_e2 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="显示"]') driver.scroll(web_element_e1, web_element_e2) # 滑动
# driver.drag_and_drop(web_element_e1, web_element_e2) # 拖动
- flick
flick(self,start_x,start_y,end_x,end_y):按住A点后快速滑动至B点,A点的坐标为start_x,start_y,B点的坐标为end_x,end_y,使用flick方法进行滑动偶尔出现不稳定,可以把flick方法中的press改为long_press解决。
import time from appium import webdriver from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) """ 在使用滑动方法的时候,大多数情况都是使用到了坐标,坐标不好的地方在于不同的手机,代码兼容性差 因为手机屏幕的分辨率不一样 """
driver.flick(720, 1910, 720, 1000) # 滑动 把 press 给成long_press 根据坐标滑动
![](https://img2020.cnblogs.com/blog/1242217/202111/1242217-20211101112141995-1941615049.png)
- swipe
swipe(self,start_x,start_y,end_x,end_y,duration=None):从A点滑动至B点,滑动时间为毫秒,A点的坐标为start_x,start_y,B点的坐标为end_x,end_y
import time from appium import webdriver from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) """ 在使用滑动方法的时候,大多数情况都是使用道了坐标,坐标不好的地方在于不同的手机,代码兼容性差 因为手机屏幕的分辨木不一样 """ driver.swipe(720, 1910, 720, 1000,3000)
![](https://img2020.cnblogs.com/blog/1242217/202111/1242217-20211101112358517-1468334234.png)
- 操作swipe进行上下左右滑动
import time from appium import webdriver from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # 从下往上 width = driver.get_window_size()['width'] height = driver.get_window_size()['height'] driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, 500) # 从上往下 width = driver.get_window_size()['width'] height = driver.get_window_size()['height'] driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, 500) # 从左往右 width = driver.get_window_size()['width'] height = driver.get_window_size()['height'] driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, 500) # 从右往左 width = driver.get_window_size()['width'] height = driver.get_window_size()['height'] driver.swipe(width * 4 / 5, height / 2, width / 5, height / 2, 500)
- TouchAction -- 辅助类讲解
TouchAction操作讲解:Appium的辅助类,主要针对手势操作,比如滑动、长按、拖动等。其原理是将一系列的动作放在一个链条中,然后将该链条传递给服务器。服务器接受到该链条后,解析各个动作,逐个执行。
TouchAction类支持的动作很多,常用哦的如下:
1、press(self,el=None,x=None,y=None):按压一个元素或坐标,le为要点击的元素,x/y为坐标
2、long_press(self,el=None,x=None,y=None,duration=1000):长按压一个元素或坐标,默认长按时间1000ms
3、tap(self,element=None,x=None,y=None,count=1):对一个元素或控件执行点击操作
4、move_to(self,el=None,x=None,y=None):将指针从上一个点移动到指定的元素或点
5、wait(self,ms=0):等待时间,单位毫秒
6、release(self):释放,结束屏幕上的一系列动作的命令操作
7、perform(self):执行,将待执行的操作发送到服务器的命令操作
- TouchAction -- press
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) # 需要先定义一个TouchAction对象 touch.press(x=720, y=1910).perform()
![](https://img2020.cnblogs.com/blog/1242217/202111/1242217-20211101114210835-1953691897.png)
- TouchAction -- long_press 长按元素
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) #touch.press(x=720, y=1910).perform() touch.long_press(x=720, y=1910).perform()
- TouchAction -- tap 对指定元素进行点击
des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) touch.tap(x=720, y=1910).perform() # 点击指定坐标
- TouchAction -- move_to
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.common.touch_action import TouchAction des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) element_01 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="屏幕锁定"]') TouchAction(driver).press(element_01).release().perform()
time.sleep(2) element_02 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="屏幕锁定"]') TouchAction(driver).press(element_02).release().perform()
time.sleep(2) element_03 = driver.find_element(By.XPATH, '//android.widget.TextView[@text="图案"]') TouchAction(driver).press(element_03).release().perform() """ 339 1287 721 1287 721 1662 721 2043 339 1665 1101 1667 """
touch_action = TouchAction(driver) touch_action.press(x=339, y=1297).wait(1000).move_to(x=721, y=1287).wait(1000).move_to(x=721, y=1662).wait( 1000).move_to(x=721, y=2043).wait(1000).move_to(x=339, y=1665).wait(1000).move_to(x=1101, y=1667).wait( 1000).release().perform()
![](https://img2020.cnblogs.com/blog/1242217/202111/1242217-20211101114839693-1072733113.png)
-
常用的输入方法
driver.find_element(By.XPATH,"xxx").sendkeys()
那我们如果是遇到非文本框输入怎么办?
可以使用第三方插件库
需要先下载 ADBKeyboard.apk
然后进行安装
def keyboard_input(self, text): """ :param text: 调用adb打开键盘,并输入内容 :return: """ try: time.sleep(2) adb1 = "adb shell ime set com.android.adbkeyboard/.AdbIME" os.system(adb1) time.sleep(2) adb2 = "adb shell am broadcast -a ADB_INPUT_TEXT --es msg '%s'" % text os.system(adb2) logger.info("当前键盘输入的内容是 % s " % text) except Exception as e: logger.error("当前操作报错,原因是: [%s]" % e) self.save_image_to_allure()
传入想输入的值既可
本次分享就到这儿,下次继续~
scroll(self,origin_el,destination_el,duration=None):模拟从元素origin_el滚动至元素destination_el