Appium模拟手势高级操作

1、前置代码:


from appium import webdriver

import time
"""
"unicodeKeyboard":True,
"resetKeyboard":True
隐藏键盘,可以直接输入中文
"""
desired_caps = {
                "platformName":"Android",
                "platformVersion":'5.1.1',
                "deviceName":"127.0.0.1:21503",
                "appPackage":"com.android.settings",
                "appActivity":".Settings",
                "unicodeKeyboard":True,
                "resetKeyboard":True
                }

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
2、tap  手指点击

  格式:tap(element=None,x=None,y=None).perform()
  说明:element:元素定位来点击
        x:相当于元素的X轴坐标
        y:相当于元素的Y轴坐标
        按坐标点击
    perform():执行命令操作

案例:进入设置,点击WLAN
代码实现:(1)通过元素定位来点击
      from appium.webdriver.common.touch_action import TouchAction  #导入TouchAction模块
      el=driver.find_element_by_xpath("//*[@text,'WLAN']")
      TouchAction(driver).tap(el).perform()
     (2)通过坐标来点击
      TouchAction(driver).tap(x=86,y=179).perform()

3、press   按压,有按压则对应则有 release:离开

  格式:press(el=None,x=None,y=None)
  案例:打开设置,按压WLAN
  代码实现:
from appium.webdriver.common.touch_action import TouchAction  #导入TouchAction模块
el=driver.find_element_by_xpath("//*[@text,'WLAN']")
TouchAction(driver).press(el).release().perform()  #通过元素定位按压
#TouchAction(driver).press(x=86,y=179).release().perform()  #通过坐标按压
4、wait  等待操作  
  格式wait(ms):单位毫秒
  案例:长按WLAN 5s
  代码实现:

from appium.webdriver.common.touch_action import TouchAction  #导入TouchAction模块
el=driver.find_element_by_xpath("//*[@text,'WLAN']")
TouchAction(driver).press(el).wait(5000).release().perform()  #通过元素定位按压5s
#TouchAction(driver).press(x=86,y=179).wait(5000).release().perform()  #通过坐标按压5s
5、long_press  长按
  
  格式:long_press(element=None,x=None,y=None,duration=None)
  说明:element:元素定位
     x:x轴坐标
     y:Y轴坐标
    duration:长按时间,单位毫秒
  案例:长按WLAN 5s
  代码实现:
    from appium.webdriver.common.touch_action import TouchAction  #导入TouchAction模块
    el=driver.find_element_by_xpath("//*[@text,'WLAN']")
    TouchAction(driver).long_press(el,duration=5000).release().perform()  #通过元素定位按压5s
    #TouchAction(driver).long_press(x=86,y=179,duration=5000).release().perform()  #通过坐标按压5s

6、move_to  手指移动
  
  格式:move_to(el=None,x=None,y=None)
  案例1:定位存储和更多,然后由存储滑到更多
  代码实现:按住存储等待1s在滑向更多


    # 定位到存储
    el = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
    # 定位到更多
    el1 = driver.find_element_by_xpath("//*[contains(@text,'更多')]")
    # 元素方式滑动(从(存储滑动到更多))
    #TouchAction(driver).press(el).wait(1000).move_to(el1).release().perform()
    # 坐标的方式滑动(从(240,600)滑动到(100,100))
    TouchAction(driver).press(x=240,y=600).wait(1000).move_to(x=100,y=100).release().perform()

案例2:设置九宫格锁
  打开设置>滑动到安全>点击安全>点击屏幕锁定方式>点击图案>绘制图案>点击继续>再次绘制图案>点击确认>点击完成

  代码实现:
    
 from appium import webdriver
      from appium.webdriver.common.touch_action import TouchAction
      from selenium.webdriver.support.wait import WebDriverWait
      import time
      xwxxh={
           'platformName':'Android',
           'deviceName':'127.0.0.1:21503',
           'platformVersion':'5.1.1',
           'appPackage':'com.android.settings',
           'appActivity':'.Settings'
           }
      driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',xwxxh)
      #定位WLAN
      el1=driver.find_element_by_xpath('//*[contains(@text,"WLAN")]')
      #定位电池
      el2=driver.find_element_by_xpath('//*[contains(@text,"电池")]')
      # 向下滑动
      driver.scroll(el2,el1)
      #点击安全进入
      time.sleep(2)
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"安全")]')).click()
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"屏幕锁定方式")]')).click()
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"图案")]')).click()
      time.sleep(3)
      TouchAction(driver).press(x=95,y=334).wait(1000).move_to(x=192,y=0).wait(1000).move_to(x=192,y=0).wait(1000).move_to(x=(-192),y=192).wait(1000).move_to(x=(-192),y=192).wait(1000).move_to(x=192,y=0).wait(1000).release().wait(1000).perform()
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"继续")]')).click()
      TouchAction(driver).press(x=95,y=334).wait(1000).move_to(x=192,y=0).wait(1000).move_to(x=192,y=0).wait(1000).move_to(x=(-192),y=192).wait(1000).move_to(x=(-192),y=192).wait(1000).move_to(x=192,y=0).wait(1000).release().wait(1000).perform()
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"确认")]')).click()
      WebDriverWait(driver,5,1).until(lambda x:x.find_element_by_xpath('//*[contains(@text,"完成")]')).click()



原文地址:https://www.cnblogs.com/xwxxh/p/12666895.html