Python3+Appium学习笔记08-元素定位
appium整合了不同的自动化测试驱动程序。而新版本appium desktop 中安卓是使用UI Automator2来作为驱动程序的。以前版本是使用UI Automator1或 Selendroid。所以参数中需要加上 'automationName': 'uiautomator2'指定驱动程序。第一次使用这个参数的时候,会安装一个sever程序。
另外就是元素操作的时候,需要打开开发者选项中的USB模拟点击权限。
定位方式大部分是和selenium一样的。小部分是安卓和ios对应驱动中特有的。
同样也有find_element(单个元素)和find_elements(多个元素)两种模式,后者定位返回的是一个数组。
1)id定位
driver.find_element_by_id()
使用UI Automator Viewer上对应resource-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.taobao.taobao',
'appActivity': 'com.taobao.tao.TBMainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
driver.find_element_by_id("com.taobao.taobao:id/home_searchedit").click()
代码效果是会打开淘宝App,然后点击搜索框
2)name定位
driver.find_element_by_name()
使用UI Automator Viewer上对应text的值
在较新的版本已经不能使用name来定位,如果使用name的api来定位会报错
3)className定位
driver.find_element_by_class_name()
tab_name定位的方法已被该方法取代
使用UI Automator Viewer上对应的class值,但是一般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.taobao.taobao',
'appActivity': 'com.taobao.tao.TBMainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
su = driver.find_elements_by_class_name("android.widget.TextView")
print(len(su))

因为classname基本都不会是唯一的,所以使用find_elements。我这边定位到42个。
4.accessibility_id定位
driver.find_element_by_accessibility_id()
官方文档说安卓是用该方法取代name定位方法
使用UI Automator Viewer上对应的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)
driver.find_element_by_accessibility_id('产品大全').click()
使用了百度app来演示。效果是打开百度后,点击右上角的产品大全按钮
5.xpath定位
driver.find_element_by_xpath()
使用xpath来定位
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)
driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TabHost/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextSwitcher/android.widget.TextView').click()
代码效果是打开百度,然后点击搜索框
官方文档和appium desktop定位工具都提示说尽量不要使用xpath去定位,不稳定。
这些方法都是在Native(大致理解手机桌面上的应用)中常用的,还有一些是webview(浏览器应用,或者打开的是个页面,H5这种)中使用的
driver.find_element_by_link_text()
driver.find_element_by_partial_link_text()
driver.find_element_by_css_selector()
如这些是在webview中使用的
另外还提供了一些定位的方法
driver.find_element_by_android_data_matcher()
driver.find_element_by_android_viewtag()
driver.find_element_by_custom()
driver.find_element_by_image()
data_matcher 是Espresso驱动程序中DataMatcher定位方式。看源码要去下载插件
viewtag 好像是安卓一个样式标签?
custom 是完全不知道干嘛的
image 是用来定位64位编码的图片。但是好像需要安装一些插件
Python3+Appium学习笔记08-元素定位的更多相关文章
- Appium学习笔记4_元素定位方法
Appium之元素定位,如果对Android上如何使用工具获取页面元素有问题的,请转战到这:http://www.cnblogs.com/taoSir/p/4816382.html. 下面主要是针对自 ...
- Python3+Appium学习笔记09-元素定位android_uiautomator
appium是使用了uiautomator的框架的,所以uiautomator所带的定位方式.appium也是支持的 需要使用appium中find_element_by_android_uiauto ...
- 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 ...
随机推荐
- 涨停复盘:5G概念持续活跃,军工股强势崛起
午后银行股快速拉升,三大股指大幅拉升,沪指一度临近2800点,但未能持续随后沪指小幅下行,题材股表现强势,证券板块高开低走.截止收盘,沪指涨0.93%,创业板指涨1.51%. 盘面上,银行板块午后拉升 ...
- 入行IT的选择决定了日后走的路的长度和领域的深度
前段时间和发小聊天时,他说了一句话我觉得很值得思考,送给大家:机遇大于努力,选择大于机遇. 一年前我毅然辞去了之前的工作,只身来到北京,正式成为了北漂的一员.对于我们现在的大环境下,其实北漂已经和以前 ...
- 【miscellaneous】理解Gstreamer架构
本文给出了Gstreamer的总体设计.通过阅读本文可以了解Gstreamer的内部工作原理.本文编译自gstreamer源码中的文档,原文在源码中的位置是/gstreamer/docs/design ...
- Npcap.例子(raw tcp syn)
1.来自:winpcap实现syn攻击 - 125096 - CSDN博客.html(https://blog.csdn.net/qq125096885/article/details/5178452 ...
- redis windows 扩展Redis igbinary 下载地址
http://windows.php.net/downloads/pecl/releases/redis/3.1.2/ http://windows.php.net/downloads/pecl/re ...
- iOS开发系列之app的一天
本文主要讲述我对 iOS 开发的一些理解,希望能通过 app 从启动到退出,将一些的知识整合起来,形成一条知识链,目前涉及到的知识点有 runloop.runtime.文件存储.界面布局.离线推送.内 ...
- TCP调试助手,十六进制发送或者字符串形式发送的理解
"无论创作还是欣赏,都是对法则和规律的逃逸,自由是艺术的源泉"-- 黑格尔 TCP调试助手中,在发送时可以选择十六进制发送或者字符串形式发送! 其实,两者最终调用的都是系统的soc ...
- Azure经典虚拟机(Windows)如何监测单个磁盘的使用空间
Azure云平台创建经典虚拟机(Windows)后,发现仪表板的监测项下默认是没有针对磁盘空间进行检测的指标的 本地机器安装Windows Azure Powershell模块,并通过如下命令登陆并查 ...
- 剑指offer60:把二叉树打印成多行。上到下按层打印二叉树。
1 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 2 思路和方法 vector变量存储每一层的元素vector<vector<int> > ans ...
- asp.net mvc抓取微信文章里面所有的图片
/// <summary> /// 下载指定URL下的所有图片 /// </summary> public class WebPageImage { /// <summa ...