APP自動化6---元素操作詳情
阿新 • • 發佈:2022-03-24
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.Button15 文字內容是登入
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