代碼
https://github.com/iffy1/AppiumTest
定位方法 1.絕對路徑(xpath,id,name, class name) 2.相對路徑 3.座標(比如視頻全屏播放界面)
1.包含使用contains
driver.find_element_by_xpath("//*[contains(@text,'我發佈的')]").click()
如果要定位下圖中的返回鍵://class[contains(@元素,'元素名稱')]
字符查找封裝
def get_bottom_button(button_name):
return "//android.widget.TextView[@text=\"%s\"]" % button_name
driver.find_element_by_xpath(dc.get_bottom_button("閒魚")).click()
driver.find_element_by_xpath("//android.widget.ImageButton[contains(@content-desc,'Navigate up')]")
2.準確查找
driver.find_element_by_xpath("//*[@content-desc='我的,未選中狀態']").click()
子view查找
driver.find_element_by_xpath("//android.widget.ScrollView/child::android.view.View[5]"))
3.find_element找不到會報錯,find_elements找不到的不會報錯,返回空列表
zhuanmai_btn = 0
try:
zhuanmai_btn = driver.find_element_by_xpath("//*[@content-desc='一鍵轉賣']")
except:
print('沒找到一鍵轉賣')
4.元素等待,隱式等待,顯式等待
使用場景:網速/服務器慢
# 10秒只能找到aa就會直接執行,不會等到10秒
# 10秒還沒知道會拋異常
#會影響後續所有find_element方法
driver.implicitly_wait(10) #全局有效 會影響所有的find語句
driver.find_element_by_name('aa')
driver.find_element_by_name('bb')
顯式等待
# 25秒超時 每5秒找1次 單個有效
WebDriverWait(driver, 25, 5).until(lambda x: x.find_element_by_xpath("//*[@text ='iffy']"), '沒找到').click()
不推薦寫死時間,特殊場景例外
time.sleep()
5.元素API
element.click()點擊
element.send_keys()寫字
element.clear()清空
element.text 獲取內容
element.location獲取位置返回字典 xy
element.size獲取大小 返回字典寬高
6.根據屬性名獲取屬性值
network = driver.find_element_by_xpath("//*[@text ='Network & internet']")
print('clickable:' + network.get_attribute('clickable'))
C:\Python38\python.exe C:/PycharmProjects/AppiumTest/getProperty.py
clickable:false
7.滑動
swipe(x_start,y_start,x_end,y_end,duration) 相同距離滑動時間越短 滑動距離越大
scroll(element_start,element_end) 滑動距離不可控
drag_and_drop(element_start,element_end) 滑動距離比較靠譜
8.高級手勢TouchAction
比如手機圖形解鎖
# 找到wlan按鈕
wlan_btn = driver.find_element_by_xpath("//*[@text ='Network & internet']")
# 創建touch action對象
ta = TouchAction(driver)
# 想要執行的動作
ta = ta.tap(wlan_btn)
# 執行
ta.perform()
# 簡寫
TouchAction(driver).tap(wlan_btn).perform()
# count 多次點擊
TouchAction(driver).tap(x=100, y=100, count=10).perform()
# 按下 擡起
TouchAction(driver).press(x=100, y=100).perform()
TouchAction(driver).press(x=100, y=100).release().perform()
# 按下後等待
TouchAction(driver).press(x=500, y=600).press().wait(100).release().perform()
# 長按
TouchAction(driver).long_press(x=500, y=600, duration=2000).release().perform()
def set_pattern_psw():
print('畫正方形')
TouchAction(driver).press(x=100, y=100)\
.move_to(x=100, y=200)\
.move_to(x=200, y=200)\
.move_to(x=200, y=100)\
.move_to(x=100, y=100)\
.perform()
9.其他API
# 獲取分辨率 {'width': 1080, 'height': 1794}
print(driver.get_window_size())
# 420
print(driver.get_display_density())
# 截圖
driver.get_screenshot_as_file(os.path.abspath("a.png"))
# 獲取網絡模式
"""
Possible
values:
Value(Alias) | Data | Wifi | Airplane
0(None) | 0 | 0 | 0
1(AirplaneMode) | 0 | 0 | 1
2(Wifionly) | 0 | 1 | 0
4(Dataonly) | 1 | 0 | 0
6(All network on) | 1 | 1 | 0
"""
print(driver.network_connection)
# 設置飛行模式
# driver.set_network_connection(4)
# driver.set_network_connection(ConnectionType.AIRPLANE_MODE)
try:
driver.set_network_connection(1)
except Exception as e:
print("截獲異常"+e.args[0])
print(driver.network_connection)
# 模擬按鍵
# 音量+
driver.press_keycode(keycode=24)
# 音量-
driver.press_keycode(keycode=24)
# home
driver.press_keycode(keycode=3)
# 操作通知欄
driver.open_notifications()
time.sleep(2)
# 返回鍵
driver.press_keycode(keycode=4)
完結
https://www.bilibili.com/video/BV1B441197rZ?p=35