Python3+Appium学习笔记09-元素定位android_uiautomator
appium是使用了uiautomator的框架的,所以uiautomator所带的定位方式。appium也是支持的
需要使用appium中find_element_by_android_uiautomator()方法
示例基本都已百度app为例
1)text定位
1.通过text文本来定位
'new UiSelector().text("对应text的值")'
2.类似模糊匹配,只要包含text内容
'new UiSelector().textContains("包含text文本")'
3.通过text开头内容来定位
'new UiSelector().textStartsWith("text开头")'
4.通过正则匹配
'new UiSelector().textMatches("正则表达式")'
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
# text = 'new UiSelector().text("搜索或输入网址")'
# driver.find_element_by_android_uiautomator(text).click()
# text1 = 'new UiSelector().textContains("输入网址")'
# driver.find_element_by_android_uiautomator(text1).click()
# text2 = 'new UiSelector().textStartsWith("搜索")'
# driver.find_element_by_android_uiautomator(text2).click()
text3 = 'new UiSelector().textMatches("^搜索.*")'
driver.find_element_by_android_uiautomator(text3).click()
四个方法效果全是点击百度app的搜索框。执行时要把另外三个给注释掉。
2)resourceId
resourceId等价于by_id
'new UiSelector().resourceId("id")'
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
text = 'new UiSelector().resourceId("com.baidu.searchbox:id/baidu_searchbox")'
driver.find_element_by_android_uiautomator(text).click()
点击百度app的搜索框
3)className
就是用class的值,等价于class_name
'new UiSelector().className("class")'
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
text = 'new UiSelector().className("android.widget.TextSwitcher")'
su = driver.find_elements_by_android_uiautomator(text)
print(len(su))
su[0].click()
点击百度app的搜索框
class一般都是有重复的,要用find_elements。这里正好是唯一的。
4)description
使用content-desc的值定位,等价于accessibility_id
'new UiSelector().description("content-desc的值")'
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
text = 'new UiSelector().description("产品大全")'
driver.find_element_by_android_uiautomator(text).click()
效果是打开百度app,点击右上角的产品大全按钮
5)组合定位
可以使用两个定位方式组合在一起来定位。一般使用id,class,text这三个属性来组合。
'resourceId("id值").text("text值")'
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
text = 'resourceId("com.baidu.searchbox:id/image_search_entrance").className("android.widget.ImageView")'
driver.find_element_by_android_uiautomator(text).click()
效果是打开百度app,点击搜索旁边的拍照按钮
6)父子层级
使用childSelector
当一个元素很难定位的时候,可以先定位到他的父元素,然后再定位到这个子元素

比如百度app的这个频道管理按钮,和频道管理平级的三个元素class全是不一样的。我们先定位到父元素,再来定位到他
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
su = driver.find_elements_by_class_name("android.widget.ImageView")
print(len(su))
text = 'resourceId("com.baidu.searchbox:id/tab_right_button_area").childSelector(className("android.widget.ImageView"))'
driver.find_element_by_android_uiautomator(text).click()
执行代码,会打开百度app,然后点击频道管理
可以看到如果直接通过class去定位,会有22个重复的元素。当先定位到父元素,再通过class去定位,能直接定位到该元素。
7)兄弟层级
使用fromParent
这个就是通过同层级的某元素,去定位另外一个同层级的元素

这次通过先定位到频道管理,然后再定位到朗读的按钮
from appium import webdriver
import time
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.1.2',
'deviceName': '1b6ca8f',
'noReset': True,
# 'app':'apk路径',
'automationName': 'uiautomator2',
'appPackage': 'com.baidu.searchbox',
'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
su = driver.find_elements_by_class_name("android.widget.LinearLayout")
print(len(su))
text = 'resourceId("com.baidu.searchbox:id/tab_right_plus").fromParent(className("android.widget.LinearLayout"))'
driver.find_element_by_android_uiautomator(text).click()
执行代码,会打开百度app,然后点击朗读按钮
可以看到如果直接通过class去定位,会有10个重复的元素。当先定位到同层级元素,再通过class去定位,能直接定位到该元素。
Uiautomator还有很多定位方式,感兴趣可以百度一下,比如id,text也可以使用正则。
Python3+Appium学习笔记09-元素定位android_uiautomator的更多相关文章
- Appium学习笔记4_元素定位方法
Appium之元素定位,如果对Android上如何使用工具获取页面元素有问题的,请转战到这:http://www.cnblogs.com/taoSir/p/4816382.html. 下面主要是针对自 ...
- Python3+Appium学习笔记08-元素定位
appium整合了不同的自动化测试驱动程序.而新版本appium desktop 中安卓是使用UI Automator2来作为驱动程序的.以前版本是使用UI Automator1或 Selendroi ...
- UI自动化学习笔记- Selenium元素定位及元素操作
一.元素定位 1. 如何进行元素定位? 元素定位就是通过元素的信息或元素层级结构来定位元素的 2.定位工具 浏览器开发者工具 3.元素定位方式 Selenium提供了八种定位元素方式 id name ...
- Python3+Appium学习笔记01-环境配置(上)
公司可能也有关于对app自动化的一些想法,让我去研究下.当然以移动互联网的热度.对于app自动化测试技术听闻已久.也一直想要去学习.正好.这次可以在工作时间中学习.emmm.希望自己能坚持把这个系列更 ...
- Python3+Appium学习笔记07-元素定位工具UI Automator Viewer
这篇主要说下如何使用UI Automator Viewer这个工具来定位元素.这个工具是sdk自带的.在sdk安装目录Tools目录下找到uiautomatorviewer.bat并启动它 如果启 ...
- Python3+Appium学习笔记05-报错及解决方法
记录一下使用期间各种报错和解决方法,毕竟搜了半天才找到解决方法.另外提示一下.优先看官方文档. 报错前面都是一样,就是说在处理的时候,服务器发生了一个未知的错误.然后才是具体报错信息 1)seleni ...
- selenium3+python3自动化测试学习之网页元素定位
selenium基础实战之定位网页元素技巧 selenium定位网页元素 find_element_by_id,find_element_by_name,find_element_by_class_n ...
- Python3+Appium学习笔记03-启动app
这个是appium相关的官方api地址:http://appium.io/docs/en/about-appium/api/ 如同selenium进行自动化测试时,需要先创建一个浏览器实例一样.在使用 ...
- Python3+Appium学习笔记02-环境配置(下)
配置所需软件及我当前使用的版本: 1)java jdk 1.8.0 2)android sdk 24.4.1 3)Python3 3.7.3 4)Appium-Python-Client 5)n ...
随机推荐
- 【ARM-Linux开发】使用QT和Gstreanmer 遇到的一些问题
1.如果出现错误,可能是在安装UCT PCRF时,相关组件不全,略举两个碰到的错误. 1)curl/curl.h:No such file or directory --可能原因是libcurl及相关 ...
- 最新 中手游java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.中手游等10家互联网公司的校招Offer,因为某些自身原因最终选择了中手游.6.7月主要是做系统复习.项目复盘.LeetCo ...
- C#使用Castle实现AOP面向切面编程
Castle.Core 本质是创建继承原来类的代理类,重写虚方法实现AOP功能.个人觉得比Autofac用着爽 使用方式比较简单,先新建一个控制台项目,然后在Nuget上搜索Castle.Core并安 ...
- Django简介 --Python Web
Python Web主流的三种框架:Django.Flask.Tornado,使用频度:Django>Flask>Tornado 一.设计模式 MVC:模型(Model).View(视图) ...
- centos 防火墙相关命令
防火墙关闭: systemctl stop firewalld systemctl disable firewalld 重启防火墙: systemctl enable firewalld system ...
- [官网]PG12发布了
PostgreSQL 12 Press Kit https://www.postgresql.org/about/press/presskit12/zh/#original_release Conte ...
- python 脚本bak文件还原mssql数据库
# -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import decimal cla ...
- CSV文件导入数据库和导出数据库
实例一: <?php $filename = 'test'; //导出文件 header("Content-type: application/vnd.ms-excel; charse ...
- 工具——eclipse debug小技巧
1.开启调试: 在代码编辑处右键单击,在弹出菜单中点击Debug As开始调试 2.几个快捷键: F5:跟入Step into, 一般会跟踪进入到调用函数的函数体,Step Over则不会跟踪进入,直 ...
- Ubuntu部署ftp服务器
Ubuntu 16.04 FTP服务器安装及配置 FTP File Transfer Protocol文件传输协议,两台计算机传送文件的协议,客户端可以通过FTP命令从服务器下载,上传文件,修 ...