4、通过uiautomatorviewer实现appium元素定位

熟悉selenium自动化的小伙伴应该知道WebDriver 提供了八种元素定位方法:

id
name
class name
tag name
link text
partial link text
xpath
css selector

appium元素定位和WebDriver略微有些差异,具体差异在哪里,下面会具体介绍,在此之前,我们先打开uiautomatorviewer.bat(在sdk/tools路径下)

appium元素定位一:通过 Id 定位

如上图所示resource-id 就是我们要找的 Id 了

使用方法:

driver.findElement(By.id("com.android.calculator2:id/digit_9"))

appium元素定位二:通过Class Name  定位

使用 ClassName 一般获得的 view 都不止一个, 所以应该需要遍历一遍得到的 views, 然后缩小搜索条件来获得目标控件。 不推荐使用

driver.findElement(By.ClassName("android.widget.Button"));

appium元素定位三:通过XPath   定位

找父标签的class

driver.findElement(By.xpath("android.widget.FrameLayout/android.support.v4.view.ViewPager/android.widget.Button"))

appium元素定位四:通过Accessibility ID  定位

这个方法属于 Appium 扩展的定位方法。Accessibility ID 在 Android 上面就等同于 contentDescription。 这个属性是方便一些生理功能有缺陷的
人使用应用程序的。 比如我们有一个 ImageView 里面放置一张颜色复杂的图片, 可能一些色弱色盲的人,分不清这张图片中画的是什么东西。 如果用户安装了辅助浏览工具比如 TalkBack, TalkBack 就会大声朗
读出用户目前正在浏览的内容。 TextView 控件 TalkBack 可以直接读出里面的内容, 但是 ImageView,TalkBack 就只能去读 contentDescription 的值, 告诉用户这个图片到底是什么。
鉴于这是一个隐藏属性, 而 Android 上用于查找控件的各种属性可能有所缺失或者有重复(比如 id重复, 比如一个 list 下面的所有项可能都是叫做“id/text1”) , 所以最佳的办法就是跟开发团队沟通好每个
view 都赋予一个唯一的 contentDescription。其实, 我们的核心是要找到元素的 contentDescription 属性。 它就是元素的 content-desc 。

driver.findElementByAccessibilityId("加").click();

appium元素定位五:通过android uiautomator   定位

AndroidUIAutomator是一个强有力的元素定位方式,它是通过Android UIAutomator类库去找元素,一个元素的任意属性都可以通过 android uiautomator 方法来进行定位, 但要保证这种定位方式的唯一性。

driver.findElementByAndroidUIAutomator("new UiSelector().text(\"+\")").click(); 

原文地址:https://www.cnblogs.com/suim1218/p/8781561.html

时间: 2024-07-30 01:05:28

4、通过uiautomatorviewer实现appium元素定位的相关文章

Appium+python移动自动化测试(四)--Monitor/uiautomatorviewer工具及元素定位方法

写在前面: 本文介绍识别元素的工具和元素的定位方法,识别元素的工具主要有uiautomator和monitor,以及Appium Inspector(Windows下小编在1.7.0之后版本可使用,1.4.16版本不行),本系列教程用的appium1.4.16本文,故暂不介绍Appium Inspector. 一.常用的识别元素的工具介绍 启动uiautomatorviewer.bat/monitor.bat.打开sdk/tools目录找到uiautomatorviewer.bat/monito

Appium——元素定位

首先介绍两种定位元素的工具,appium自带的 Inspector 和 android SDK自带的 uiautomatorviewer 1.UIAutomator Viewer比较简单,在模拟器打开app后,打开它,选择 点击这个按钮,就会同步模拟器或真机界面 点击页面中的元素,在右下角会显示该元素的一些信息 2.Inspector(android) 使用inspector就比较麻烦了,先设置appium ①首先是android setting 第一次设置的时候 package和launch

appium元素定位

通过id定位 安卓里面的id即为resource-id driver.findElementById("com.tencent.mm:id/do") 或者 WebElement element = driver.findElement(By.id("com.tencent.mm:id/do")); 通过name定位 一般text为name WebElement el = driver.findElement(By.name("昵称")); 通过c

appium元素定位之AndroidUiAutomator

UIAutomator 元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更好用,并且支持元素全部的属性定位,定位原理是通过 android 自带的android uiautomator 的类库去查找元素 方法名:find_element_by_android_uiautomator('uiSelector 表达式') 该方法的参数为 UiSelector 类定位元素的表达式:new UiSelector().函数名称("定位表达式") 实例化一个 U

Appium元素定位难点:tap坐标定位不准确

tap用法 1.tap是模拟手指点击页面上元素语法有两个参数,第一个是positions,是list类型最多五个点,duration是持续时间,单位毫秒 tap(self, positions, duration=None): Taps on an particular place with up to five fingers, holding for a certain time 模拟手指点击(最多五个手指),可设置按住时间长度(毫秒) :Args: - positions - list类型

Appium元素定位难点:混合式的native+webview

现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了. 1.认识识webview >用uiautomatorviewer定位工具查看app页面,发现页面上有些区域无法定位到,如下图左边红色区域,只能定位到这个大框框,红色框里面的元素是无法识别的. >这时候可以查看元素属性,如右图它的class属性,上面写着WebView,那毫无疑问这种页面就是webview了. 2..接触con

学习 APPIUM 元素定位 (JAVA)-xpath

表达式 1.xpath根据text内容查找元素 //view[contains(@text,"value")] //view[@text="value"]等 2.xpath根据content-desc查找元素 //view[contains(@content-desc,"value")] 3.xpath根据index查找元素 //view[contains(@index,value)] 4.元素顺序 //view/view[n]/view等 例子

appium 元素定位find_element_by_android_uiautomator方法使用

若appium中给定的方法无法满足你的需求,刚好uiautomator中的方法可以满足你的需求时,你可使用find_element_by_android_uiautomator来调用uiautomator中的方法来实现. appium底层文件webdriver中给出的说明如下: def find_element_by_android_uiautomator(self, uia_string): """Finds element by uiautomator in Androi

appium 元素定位

查找控件的方式: 1.通过id查找 self.driver.find_element_by_id("com.guokr.mentor:id/text_view_topic_title") self.driver.find_elements_by_id("")[0] 2.通过name查找 self.driver.find_element_by_name("推荐") 3.通过class_name查找 self.driver.find_elements