Appium自动化(一)--location与size获取元素坐标

Appium做app自动化测试过程中,有时需要获取控件元素的坐标进行滑动操作。appium中提供了location方法获取控件元素左上角的坐标,再通过size方法获取控件元素的宽高,就可以得到控件元素更多的坐标。

一、获取坐标元素的方法

1.size() 获取元素的宽、高
el_size = driver.find_element_by_xxx('xxx').size

#元素宽
width = el_size['width']
#元素高
height = el_size['height']

2.location() 获取元素左上角坐标
el = driver.find_element_by_xxx('xxx').location

#元素左上角横坐标
x = el['x']
#元素纵坐标
y = el['y']

3.由此计算出元素的其他坐标
(x+width,y) # 右上角坐标
(x+width,y+height) # 右下角坐标
(x,y+height) # 左下角坐标
(x+width/2,y+height/2) # 元素中心点坐标

二、使用场景
创业天下APP--创业--设置--更改密码--修改密码图像

代码如下:

from appium import webdriver
from appium.webdriver.common import mobileby
import time,base64
from appium.webdriver.common.touch_action import TouchAction

def init_driver():
    des = {
            'platformName': 'Android',
            'deviceName': 'emulator-5554',
            "platformVersion": '5.1.1',
            'appPackage': 'com.sxdachun.entrepreneurial_world',
            'appActivity': 'com.sxdachun.entrepreneurial_world.mvp.ui.activity.MainActivity',
            'unicodeKeyboard': True,  # 使用unicode编码方式发送字符串
            'resetKeyboard': True,  # 将键盘隐藏起来
            'noReset': True,
    }

    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',des)
    return driver

driver = init_driver()
by = mobileby.MobileBy()

# 点击“创业”模块
tab_chuangye = (by.ID,"com.sxdachun.entrepreneurial_world:id/iv_tab_chuangke")
driver.find_element(*tab_chuangye).click()
time.sleep(2)

# 点击“设置”按钮
button_settings = (by.ID,"com.sxdachun.entrepreneurial_world:id/iv_right")
driver.find_element(*button_settings).click()
time.sleep(2)

# 点击“账号安全”栏
button_phone_secure = (by.ID,"com.sxdachun.entrepreneurial_world:id/rl_account_safe")
driver.find_element(*button_phone_secure).click()
time.sleep(2)

# 点击“修改创业密码”
change_password = (by.ID,"com.sxdachun.entrepreneurial_world:id/rl_change_img_pwd")
driver.find_element(*change_password).click()
time.sleep(2)

old_pwd = (by.ID,"com.sxdachun.entrepreneurial_world:id/lockviewExpand")
# 获取该元素宽高
size = driver.find_element(*old_pwd).size
width = size.get("width")
height = size.get("height")

# 获取该元素的左上角坐标
location = driver.find_element(*old_pwd).location
size_x = location.get("x")
size_y = location.get("y")
print(width,height,size_x,size_y)

"""
模拟9宫格1~9键
1:move_to(x = size_x + width*1/10 , y = size_y + height*1/10).wait(100)
2:move_to(x = size_x + width*1/2 , y = size_y + height*1/10).wait(100)
3:move_to(x = size_x + width*9/10 , y = size_y + height*1/10).wait(100)
4:move_to(x = size_x + width*1/10 , y = size_y + height*1/2).wait(100)
5:move_to(x = size_x + width*1/2 , y = size_y + height*1/2).wait(100)
6:move_to(x = size_x + width*9/10 , y = size_y + height*1/2).wait(100)
7:move_to(x = size_x + width*1/10 , y = size_y + height*9/10).wait(100)
8:move_to(x = size_x + width*1/2 , y = size_y + height*9/10).wait(100)
9:move_to(x = size_x + width*9/10 , y = size_y + height*9/10).wait(100)
"""

# 点击--移动--释放
# 输入原始密码 147
TouchAction(driver).\
    press(x = size_x + width*1/10 , y = size_y + height*1/10).wait(100).\
    move_to(x = size_x + width*1/10 , y = size_y + height*1/2).wait(100).\
    move_to(x = size_x + width*1/10 , y = size_y + height*9/10).wait(100).\
    release().perform()
time.sleep(2)

# 输入新密码258
TouchAction(driver).\
    press(x = size_x + width*1/2 , y = size_y + height*1/10).wait(100).\
    move_to(x = size_x + width*1/2 , y = size_y + height*1/2).wait(100).\
    move_to(x = size_x + width*1/2 , y = size_y + height*9/10).wait(100).\
    release().perform()
time.sleep(2)

# 再次输入新密码258
TouchAction(driver).\
    press(x = size_x + width*1/2 , y = size_y + height*1/10).wait(100).\
    move_to(x = size_x + width*1/2 , y = size_y + height*1/2).wait(100).\
    move_to(x = size_x + width*1/2 , y = size_y + height*9/10).wait(100).\
    release().perform()
time.sleep(2)
讨论数量: 0

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!