新版appium绘制九宫格的一个注意点

在用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 webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy from appium.webdriver.common.touch_action import TouchAction from 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.location ta.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()

 

======2020年4月28日更新======

今天看官方文档,看到这一句话:也就是说当moveTo中有元素对象时,传的坐标是相对于元素位置的相对坐标,没有元素对象时,是绝对坐标

Appium 客户端库有不同的实现方式,例如:你可以传递坐标或一个元素给 moveTo 事件。当坐标 元素一起传递时,该坐标被理解为是相对于元素位置的相对坐标,而不是绝对位置


__EOF__

本文作者cnhkzyy
本文链接https://www.cnblogs.com/my_captain/p/9341937.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   cnhkzyy  阅读(906)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示