小伙伴们都知道如果是web端可以通过F12进行查看元素(右击检查查看元素)那么app如何查看呢?app的通过uiautomatorviewer工具进行元素定位,然后获取对应的一些操作。uiautomatorviewer是Android-sdk自带的元素定位工具。

1.打开uiautomatorviewer工具:android-sdk-windows\tools下

android 怎么把元素放新增到第一位 android 元素定位_自动化测试

2.任意点击下图中按钮,获取app页面属相



android 怎么把元素放新增到第一位 android 元素定位_自动化测试_02

3.通过移动鼠标放到想要定位的位置,然后查看右下角元素属性



android 怎么把元素放新增到第一位 android 元素定位_自动化测试_03

android 怎么把元素放新增到第一位 android 元素定位_元素定位_04

元素定位


1、通过id定位

# 格式

find_element_by_id()

点击搜索框,查看右下角元素属性



android 怎么把元素放新增到第一位 android 元素定位_自动化测试_05

属于id属性,进行点击等操作


# 通过ID获取属性 driver.find_element_by_id('com.taobao.taobao:id/home_searchedit').click()

2、通过class_name定位

# 格式

find_element_by_class_name()

继续拿上面的图我们做分析,发现有class=‘android.widget.EditText’

# 通过class_name进行定位

find_element_by_class_name('android.widget.EditText')

3、通过text定位

# 格式

find_element_by_link_text()

上图发现text属性有值为text=‘小米cc9e钢化膜’

# 通过text进行定位

find_element_by_link_text('小米cc9e钢化膜')

4、通过xpath定位

# 格式

find_element_by_xpath()

# xpath也可以通过id,class,name进行定位

# 通过id

find_element_by_xpath('//*[@resource-id='属性值']')

# 通过class

find_element_by_xpath('//*[@class='属性值']')

# 通过name

find_element_by_xpath('//*[@name='属性值']')

# 其他属性

find_element_by_xpath('//标签下[@index='属性值']')

5、通过name定位

# 格式

find_element_by_name()

# 这个工具上好像没有name属性,我们可以在web查看试试

6、通过tab_name定位

# 格式

find_element_by_tag_name()

7、通过css进行定位

# 格式

find_element_by_css_selector()

# css也可以通过其他属性定位

# 通过id

find_element_by_css_selector('#id属性')

# 通过标签定位,尽量不要用,重复的标签太多了,可以和其他属性一起使用

find_element_by_css_selector('标签名#其他属性')

# 通过class

find_element_by_css_selector('.class属性')

说明:在CSS中定位id属性前面要加"#",在class属性面前需要加“.”