Appium python 定位元素
代码
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
Appium python 定位元素相关推荐
- python元素定位input button_selenium+python 定位元素方法
元素定位主要方法: id定位:find_element_by_id(' ') name定位:find_element_by_name(' ') class定位:find_element_by_clas ...
- python定位元素在列表中的位置_python定位列表元素
Python 列表(list)提供了 index() 和 count() 方法,它们都可以用来查找元素. index() 方法 index() 方法用来查找某个元素在列表中出现的位置(也就是索引),如 ...
- appium无法定位元素,TouchAction坐标定位工具
在appium使用元素定位时 uiautomatorviewer定位如下图,这里看到的坐标有时候是不准的,我们要开启手机开发者选项的指针位置 在开发者选项中开启显示触摸操作和指针位置,这样以后调试ap ...
- python定位元素_Python元素定位
一. id元素定位 id有两种情况:一种id是唯一的,另一种id是动态的. (1)当id是唯一,最简单的定位方式: 用法:find_element_by_id("id_value" ...
- python定位方法_selenium+python定位元素方法
C语言 · 最小乘积(基本型) 问题描述 给两组数,各n个. 请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小.要求程序输出这个最小值. 例如两组数分别为: ...
- [Appium] App自动化-元素定位
[Appium] App自动化-元素定位及工具 一.元素定位工具简介 Web自动化是通过浏览器自带的F12键进行元素定位,但是App自动化支持三大定位工具(UIAutomatorView/Appium ...
- appium+python自动化测试~~~~~~~问题记录
先不说安装,先说安装遇到的问题 1,问题描述: 先是安装了原来常用的appium-server,该版更新止步于1.4.16版,已经两年多未更新,并且发现不支持更高版本的安卓API,仅支持到API23 ...
- appium python 抓包_利用appium自动控制移动设备并抓取数据
利用appium自动控制移动设备并提取数据 学习目标 了解 appium-python-client模块定位元素以及提取其文本内容的方法 了解 appium-python-client模块控制滑动动作 ...
- python android自动化元素定位_linux下Appium+Python移动应用自动化测试实战---3.手把手教你定位元素编写测试用例...
linux下Appium+Python移动应用自动化测试实战-3.手把手教你定位元素编写测试用例 前言 有很多童鞋环境搭建好了却没有进行下一步,是因为缺少step by step的资料. 互联网上ap ...
最新文章
- 多个微服务控制台的多窗口展示
- golang中的nil
- 机房收费--上机状态查询
- 【LUA table 移除操作非常慢】
- Mac OS X snow leopard 10.6 VMware安装
- 第七篇:Spring Boot整合Thymeleaf_入门试炼02
- MySQL 优化 —— MySQL 如何使用索引
- window下的免安装redis
- 压力测试工具之DDos-Attack
- 编译原理常用简称或英文原称(思维导图形式)
- java中依赖_java中依赖、关联、聚合
- linux中-f的含义,linux 下shell中if的“-e,-d,-f”的含义
- Kubernetes Pod Evicted
- Java有参构造方法和无参构造方法详解
- python 邮件_Python发送邮件(常见四种邮件内容)
- 《黑天鹅》black swan 高清迅雷下载 DVD BD高清中英双字
- f40c5a53ba8e7e46c290769dbd291f33
- python之sympy库--数学符号计算与绘图必备
- 深度学习之激活函数小结
- C语言出生日期输入输出
热门文章
- 基于Nokia S60的游戏开发之二
- 拦截Windows消息
- 大数据之HBase教程
- python-opencv图像处理之用于跟踪的Shi-Tomasi拐角探测器
- Hepatology | 朱黎琴/于吉洋团队合作揭示新生儿肝脏发育中肝母细胞瘤转移的新机制...
- 博士女友的朋友圈都藏着什么秘密?
- 狗狗1岁相当于人类31岁!基于基因组甲基化图谱建立首个跨物种年龄转换公式...
- ubuntu下安装latex
- anaconda和python的区别_anaconda和python区别
- 小学C++编程入门书籍及相关资料介绍(二) 算法篇