APP自动化6---元素操作详情
APP常用的操作
1.常用的四大操作
- 点击 click()
- 输入文本 send_keys()
- 获取属性 get_attribute()
- 获取文本 text
1 # 点击 2 driver.find_element(MobileBy.ID,'com.tal.kaoyan:id/tv_ok').click() 3 # 输入文本 4 ele_pwd = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("请输入验证码")').send_keys('2222') 5 6 ele_p = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("登录").resourceId("com.tal.kaoyan:id/loginCodeLoginBtn")') 7 # 获取属性 8 attr=ele_p.get_attribute("className") 9 # 获取文本 10 text=ele_p.text 11 print('属性是{}'.format(attr)) 12 print('文本内容是{}'.format(text)) 13 # 打印结果 14 属性是android.widget.Button 15 文本内容是登录
2.区别于web端的操作
- 滑屏
- 长按
- 按键操作,back,home,recent以及power键等.....
2.1 滑屏操作 swipe()
调用函数def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
Args:
start_x: x-coordinate at which to start---开始位置X坐标
start_y: y-coordinate at which to start---开始位置Y坐标
end_x: x-coordinate at which to stop---结束位置X坐标
end_y: y-coordinate at which to stop---结束位置Y坐标
duration: time to take the swipe, in ms.---延时时间,ms
1 import time 2 3 from appium import webdriver 4 from appium.webdriver.common.mobileby import MobileBy 5 from selenium.webdriver.support.wait import WebDriverWait 6 from selenium.webdriver.support import expected_conditions as EC 7 8 # 1 设置终端参数 9 desired_caps = { 10 "platformName": "Android", 11 "platformVersion": 9, 12 "deviceName": "Android", 13 "appPackage": "com.xxzb.fenwoo", 14 "appActivity": "com.xxzb.fenwoo.activity.addition.WelcomeActivity", 15 "noReset":False} 16 # 2 启动appium 17 # 3 发送指令给appium driver 18 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) 19 20 # 获取屏幕大小 21 screen_size = driver.get_window_size() 22 x = screen_size['width'] 23 y = screen_size['height'] 24 time.sleep(3) 25 # 左滑 结束X>开始X,Y位置可以不变(也可以改变,看自己需求) 26 for i in range(0,3): # 左滑三次 27 print('**************') 28 driver.swipe(start_x=x*0.9, 29 start_y=y*0.5, 30 end_x=x*0.1, 31 end_y=y*0.5, 32 duration=1000) 33 34 # 等待 35 WebDriverWait(driver,10).until(EC.visibility_of_element_located((MobileBy.ID,'com.xxzb.fenwoo:id/btn_start'))) 36 # 点击立即体验 37 driver.find_element(MobileBy.ID,'com.xxzb.fenwoo:id/btn_start').click() 38 driver.quit()
2.2 长按
- 在某一个坐标长按tap函数
# 长按3秒 driver.tap(positions=[(x*0.5,y*0.5)],duration=3000)
- 长按某一个元素,long_press函数1 # 长按
2 el = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("搜狗浏览器")') 3 TouchAction(driver).long_press(el).perform()
- 长按某一个按键
# 长按home键 driver.long_press_keycode(3)
2.3 按键输入
# 按home键退出 driver.keyevent(3)
按键keycode查询----https://www.cnblogs.com/kuailede/p/13172504.html
本文来自博客园,作者:大头~~,转载请注明原文链接:https://www.cnblogs.com/xiaoying-guo/p/16049185.html