代码
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