appium+python自动化31-android_uiautomator定位
前言
appium就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法也可以用
text
1.通过text文本定位语法
new UiSelector().text("text文本")
2.文本比较长的时候,可以用textContains模糊匹配,只要文本包含匹配内容就可以了。
new UiSelector().textContains("包含text文本")
3.textStartsWith是以某个文本开头的匹配
new UiSelector().textStartsWith("以text文本开头")
4.正则匹配textMatches,这个需要配合正则表达式,就不举例了。
new UiSelector().textMatches("正则表达式")
# coding:utf-8
from appium import webdriver
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '4.4.2',
'appPackage': 'com.baidu.yuedu',
'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
'noReset': 'true',
'resetKeyboard': 'true',
'unicodeKeyboard': 'true'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主页面activity出现
driver.wait_activity(".base.ui.MainActivity", 10)
# 1.text
loc_text = 'new UiSelector().text("图书")'
driver.find_element_by_android_uiautomator(loc_text).click()
# # 2.textContains
# loc_textContains = 'new UiSelector().textContains("图")'
# driver.find_element_by_android_uiautomator(loc_textContains).click()
# # 3.textStartsWith
# loc_textStart = 'new UiSelector().textStartsWith("图")'
# driver.find_element_by_android_uiautomator(loc_textStart).click()
resourceId
1.resourceId根by_id一样
new UiSelector().resourceId("id")
# resourceId定位
loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'
driver.find_element_by_android_uiautomator(loc_id).click()

className
1.页面上的class属性一般不唯一,多半用在复数定位时候。比如通过class属性定位'排行'这个按钮下标就是2。
new UiSelector().className("className")
# className复数定位
loc_class = 'new UiSelector().className("android.widget.TextView")'
driver.find_elements_by_android_uiautomator(loc_class)[2].click()

description
1.由于这个app的contenet-des属性都是空的,就不用代码演示了,跟上面方法一样。
new UiSelector().description("contenet-des属性")
参考代码
# coding:utf-8
from appium import webdriver
from time import sleep
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '4.4.2',
'appPackage': 'com.baidu.yuedu',
'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
'noReset': 'true',
'resetKeyboard': 'true',
'unicodeKeyboard': 'true'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主页面activity出现
driver.wait_activity(".base.ui.MainActivity", 10)
# 1.text
loc_text = 'new UiSelector().text("图书")'
driver.find_element_by_android_uiautomator(loc_text).click()
# # 2.textContains
# loc_textContains = 'new UiSelector().textContains("图")'
# driver.find_element_by_android_uiautomator(loc_textContains).click()
# # 3.textStartsWith
# loc_textStart = 'new UiSelector().textStartsWith("图")'
# driver.find_element_by_android_uiautomator(loc_textStart).click()
sleep(2)
# resourceId定位'小说'
loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'
driver.find_element_by_android_uiautomator(loc_id).click()
sleep(2)
# className复数定位
loc_class = 'new UiSelector().className("android.widget.TextView")'
driver.find_elements_by_android_uiautomator(loc_class)[2].click()
在学习过程中有遇到疑问的,可以appium+python QQ群交流:330467341
appium+python自动化31-android_uiautomator定位的更多相关文章
- appium+python自动化50-生成定位对象模板templet(jinja2)
前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...
- Appium+python自动化4-元素定位uiautomatorviewer
前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...
- Appium+python自动化12-appium元素定位
前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性
- appium+python自动化49-yaml管理定位元素
前言 如何高效管理定位元素,这个是很有学问的问题,也是面试必问的[以下纯属个人观点,勿喷!]. 有的人用xml管理页面定位元素,这种逼格略高,但是小编认为学习成本大,贼麻烦. 有的人提到用excel管 ...
- Appium+python自动化12-appium元素定位【转载】
前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性
- Appium+python自动化4-元素定位uiautomatorviewer【转载】
前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...
- Appium+python自动化
名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...
- Appium+python自动化8-Appium Python API
Appium+python自动化8-AppiumPython API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...
- appium+python自动化52-多点触控MultiAction
前言 MultiAction是针对多点触控操作的,是TouchAction的一个补充模块 TouchAction用法参考前面的一篇:appium+python自动化33-TouchAction 多点触 ...
- Appium+python自动化20-查看iOS上app元素属性
前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素 Mac版的appium1.6的 ...
随机推荐
- C# 设计模式巩固 - 抽象工厂模式
前言 继续工厂模式高级版,抽象工厂模式.抽象工厂模式其实是从工厂方法模式拓展而来.在实际的生活中一个工厂的产品不可能是单一的,肯定是多种系列的产品. 介绍 - 抽象工厂模式 定义:(摘自百度百科~ ...
- 【LeetCode 28_字符串_匹配】Implement strStr()
解法一:Brute-force int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle ...
- C++11_shared_ptr
版权声明:本文为博主原创文章,未经博主允许不得转载. shared_ptr智能指针的一种,它的使用类似于auto_ptr. shared_ptr它有两个指针,一个指向引用计数,一个指向data.由于拥 ...
- MoreEffectiveC++Item35(效率)(条款16-24)
条款16 谨记80-20法则 条款17 考虑使用 lazy evaluation(缓释评估) 条款18 分期摊还预期的计算成本 条款19 了解临时对象的来源 条款20 协助完成"返回值的优化 ...
- c# 文件日志处理 需要log4net配置
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- nginx Linux 开启自启
编辑 /etc/init.d/nginx 添加一下内容 根据路劲修改. 添加可执行权限 chmod +x /etc/init.d/nginx 开启自启 chkconfig nginx on ...
- wordpress 生成自定义 meta box
工具 https://jeremyhixon.com/tool/wordpress-meta-box-generator/ 使用 生成代码 /** * Generated by the WordPre ...
- C语言中库函数strstr的实现
在C语言中库函数strstr()函数表示在一个字符串str1中查找另一个字符串str2,如果查到则返回str2在str1中首次出现的位置,如果找不到则返回null. char* strstr(char ...
- BZOJ1113 Poi2008 海报PLA【单调栈】【水】
BZOJ1113 Poi2008 海报PLA Description N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. Input 第一行给出数字N,代表有N个矩形.N在[1,250 ...
- js去重复和取重复数据
js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...