『與善仁』Appium基礎 — 21、元素的基本操作
阿新 • • 發佈:2021-12-11
目錄
1、元素的基本操作說明
(1)點選操作
點選操作:click()
方法。(同Selenium中使用方式一致)
(2)清空操作
清空操作:clear()
方法。(同Selenium中使用方式一致)
(3)輸入操作
輸入操作:send_keys()
方法。
在移動端的輸入操作有兩種情況,一種情況是輸入非中文內容,另一種情況是輸入中文內容。
1)輸入英文
使用方法:
# value:需要傳送到輸⼊框內的⽂本
send_keys(vaule)
業務場景:
- 開啟設定。
- 點選搜尋按鈕。
- 輸入內容abc。
程式碼實現:
# 點選搜尋按鈕 driver.find_element_by_id("com.android.settings:id/search").click() # 定位到輸入框並輸入abc driver.find_element_by_id("android:id/search_src_text").send_keys("abc") # 重點: # 大家可以將輸入的abc改成輸入中文內容, # 得到的結果:輸入框無任何值輸入,且程式不會抱錯。
2)輸入中文
server
啟動引數增加兩個引數配置。
也就是Desired capabilities
物件新增兩個配置引數:
# 啟用Unicode輸入法,設定為true可以輸入中文字元,預設為false
desired_caps['unicodeKeyboard'] = True
# 在設定了`unicodeKeyboard`關鍵字執行Unicode測試結束後,將鍵盤重置為其原始狀態
# 如果單獨使用resetKeyboard引數,程式碼將會被忽略,
# 因為預設值`false`,重置也的值也是`false`
desired_caps['resetKeyboard'] = True
再次執行會發現執行成功。
# 點選搜尋按鈕
driver.find_element_by_id("com.android.settings:id/search").click()
# 定位到輸入框並輸入’顯示’
driver.find_element_by_id("android:id/search_src_text").send_keys("顯示")
2、綜合練習
""" 1.學習目標 掌握appium元素點選和輸入方法 2.操作步驟 2.1 點選 元素.click() 2.2 輸入 元素.send_keys("輸入內容") 輸入會分成兩種情況: 1)輸入非中文: send_keys("WLAN") 2)輸入中文: 需要在啟動引數中新增2個引數 # 啟用Unicode輸入法,設定為true可以輸入中文字元,預設為false "unicodeKeyboard":True, # 在設定了`unicodeKeyboard`關鍵字執行Unicode測試結束後,將鍵盤重置為其原始狀態 "resetKeyboard":True 2.3 清空 元素.clear() 3.需求 在設定APP中進行搜尋操作 """ # 1.匯入appium import time from appium import webdriver # 2.建立Desired capabilities物件,新增啟動引數 desired_caps = { "platformName": "Android", # 系統名稱 "platformVersion": "7.1.2", # 系統版本 "deviceName": "127.0.0.1:21503", # 裝置名稱 "appPackage": "com.android.settings", # APP包名 "appActivity": ".Settings", # APP啟動名 "unicodeKeyboard": True, # 啟用Unicode輸入法,設定為true可以輸入中文字元,預設為false "resetKeyboard": True # 在設定了`unicodeKeyboard`關鍵字執行Unicode測試結束後,將鍵盤重置為其原始狀態 } # 3.啟動APP driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # 4.定位元素 # 4.1 定位搜尋按鈕,通過accessibility_id方法,並點選開啟 search = driver.find_element_by_accessibility_id("搜尋設定") search.click() # 4.2 定位搜尋輸入框 box = driver.find_element_by_id("android:id/search_src_text") # 4.3 輸入內容 # box.send_keys("WLAN") # 輸入英文 box.send_keys("abcdef123/*-+;") # 輸入非中文 # 清空輸入框 time.sleep(3) box.clear() # 輸入中文 box.send_keys("藍芽") # 5.關閉APP time.sleep(3) driver.quit()