在用appium-desktop-setup-1.6.2进行app手势密码设置时,发现move_to(x, y)相对偏移量的方法用不了,绘制的手势也是乱跑
还会抛一个错误
selenium.common.exceptions.InvalidCoordinatesException: Message: Coordinate [x=-210.0, y=210.0] is outside of element rect: [0,0][720,1280]
代码如下
from appium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom appium.webdriver.common.mobileby import MobileByfrom appium.webdriver.common.touch_action import TouchActionfrom time import sleep# 由你主动来告诉appium server,我要操作哪个设备上的哪个app# Desired Capabilites,键值对,键名都是已经定义好的# 操作对象的信息准备desired_caps={}# 使用的手机操作系统desired_caps["platformName"] = "Android"# 手机操作系统的版本desired_caps["platformVersion"]="5.1.1"# 使用的设备名字或模拟器的类型desired_caps["deviceName"]="Android Emulator"# app信息desired_caps["appPackage"]="com.xxzb.fenwoo"#进入的界面desired_caps["appActivity"] = ".activity.addition.WelcomeActivity"# 连接appium server,并告诉其要操作的对象driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)# 获取屏幕尺寸size=driver.get_window_size()for x in range(0,4): # 向左滑动 driver.swipe(size["width"]*0.9,size["height"]*0.5,size["width"]*0.1,size["height"]*0.5,500) # 向右滑动 # driver.swipe(size["width"]*0.1,size["height"]*0.5,size["width"]*0.9,size["height"]*0.5,500) sleep(1)# 点击立即体验WebDriverWait(driver,20,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_start")))driver.find_element_by_id("com.xxzb.fenwoo:id/btn_start").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_login")))# 点击登录注册按钮driver.find_element_by_id("com.xxzb.fenwoo:id/btn_login").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/et_phone")))# 输入用户名driver.find_element_by_id("com.xxzb.fenwoo:id/et_phone").send_keys("XXXXXX")# 点击下一步driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/et_pwd")))# 输入密码driver.find_element_by_id("com.xxzb.fenwoo:id/et_pwd").send_keys("XXXXXX")# 点击确定按钮driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_confirm")))# 马上设置driver.find_element_by_id("com.xxzb.fenwoo:id/btn_confirm").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/btn_gesturepwd_guide")))# 立即创建driver.find_element_by_id("com.xxzb.fenwoo:id/btn_gesturepwd_guide").click()# 等待WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/right_btn")))# 确定按钮driver.find_element_by_id("com.xxzb.fenwoo:id/right_btn").click()# 手势绘制页面ta=TouchAction(driver)# 获取九宫格的起点坐标和大小ele=driver.find_element_by_id("com.xxzb.fenwoo:id/gesturepwd_create_lockview")size=ele.size# 获取坐标start_point=ele.locationta.press(x=start_point["x"]+size["width"]*1/6,y=start_point["y"]+size["height"]*1/6).wait(200).\ move_to(x=size["width"]*2/6,y=0).wait(200).\ move_to(x=size["width"]*2/6,y=0).wait(200).\ move_to(x=-size["width"]*2/6,y=size["width"]*2/6).wait(200).\ move_to(x=0,y=size["width"]*2/6).wait(200).release().wait(200).perform()# 等待# WebDriverWait(driver,30,1).until(EC.visibility_of_element_located((MobileBy.ID,"com.xxzb.fenwoo:id/right_btn")))# 点击继续driver.find_element_by_id("com.xxzb.fenwoo:id/right_btn").click()
而且一个奇怪的现象是第一个圆圈的坐标点是(150, 379),相对偏移(210, 0)之后得到的坐标是(210, 640)
后来尝试着
ta.press(x=0, y=0).release().perfrom()
神奇的发现(0, 0)点竟然是(360, 640)这个位置
如果尝试press(x=0, y=10)得到的坐标点是(360, 10), press(x=10, y=0),得到的坐标点是(10, 640),只有当press(x=10, y=10)的时候得到的才是(10, 10),这样就可以解释上面x=210, y=0, 为什么是(210, 640)了。看来x或y不能为0,不然会跑偏
这个问题目前已经报bug,bug地址为:https://discuss.appium.io/t/set-gesture-password-and-show-pressed-coordinate-error-using-1-6-2-appium-desktop-on-android-simulator/22858
目前的解决办法是利用绝对坐标定位,修改的代码如下:
ta.press(x=start_point["x"]+size["width"]*1/6,y=start_point["y"]+size["height"]*1/6).wait(200).\ move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["height"]*1/6).wait(200).\ move_to(x=start_point["x"]+size["width"]*5/6, y=start_point["y"]+size["height"]*1/6).wait(200).\ move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["width"]*3/6).wait(200).\ move_to(x=start_point["x"]+size["width"]*3/6, y=start_point["y"]+size["width"]*5/6).wait(200).release().wait(200).perform()