模拟手指轻点、长按、滑动屏幕(七)

from init_driver.Init_driver import init_driver
import time
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

driver = init_driver()

# 通过元素定位,模拟手指轻敲屏幕
e1 = driver.find_element_by_xpath("//*[contains(@text, '更多')]")
TouchAction(driver).tap(el).perform()
# 通过坐标定位,模拟手指轻敲屏幕
e2 = driver.find_element_by_xpath("//*[contains(@text, '更多')]").location
TouchAction(driver).tap(x=e2.get("x"), y=e2.get("y")).perform()

# 通过元素定位,模拟手指按下屏幕和松开手指
e3 = driver.find_element_by_xpath("//*[contains(@text, '更多')]")
TouchAction(driver).press(e3).release().perform()
# 通过坐标定位,模拟手指轻敲屏幕
e2 = driver.find_element_by_xpath("//*[contains(@text, '更多')]").location
TouchAction(driver).press(x=e2.get("x"), y=e2.get("y")).release().perform()

# 模拟手指按下屏幕,等待一段时间,松开手指---第一种方法
#
e3 = driver.find_element_by_xpath("//*[contains(@text, '更多')]")
TouchAction(driver).press(e3).wait(10000).release().perform()
# 模拟手指按下屏幕,等待一段时间,松开手指---第二种方法,元素定位
e3 = driver.find_element_by_xpath("//*[contains(@text, '更多')]")
TouchAction(driver).long_press(el=e3, duration=10000).release().perform()
# 第二种方法,坐标定位
e3 = driver.find_element_by_xpath("//*[contains(@text, '更多')]").location
TouchAction(driver).long_press(x=e3.get("x"), y=e3.get("y"), duration=10000).release().perform()

# 使用显示等待
def wait_element(xpt):
    wait_w = WebDriverWait(driver, 5, 0.5).until(lambda x: x.find_element_by_xpath(xpt))
    return wait_w
# 进入图案页面
el_s = wait_element("//*[contains(@text, '声音和振动')]")
el_t = wait_element("//*[contains(@text, '飞行模式')]")
driver.drag_and_drop(el_s, el_t)
wait_element("//*[contains(@text, '密码与安全')]").click()
wait_element("//*[contains(@text, '屏幕锁定方式')]").click()
wait_element("//*[contains(@text, '图案密码')]").click()
# 绘制手势, 坐标定位移动  1:285,836   2:539,836   3:539,1092   4:285,1089
TouchAction(driver).press(x=285, y=836).move_to(x=254, y=0).move_to(x=0, y=256)
    .move_to(x=-254, y=0).release().perform()
# 元素定位移动
TouchAction(driver).press(el_t).move_to(el_s).release().perform()
# 坐标定位移动
TouchAction(driver).press(x=221, y=1660).move_to(x=0, y=-1015).release().perform()

time.sleep(5)
driver.quit()
原文地址:https://www.cnblogs.com/zhaoquanmo/p/10751079.html