在咱们作自动化测试的过程当中,最基本的就是要会元素定位,也是自动化中的灵魂所在,若是一个自动化测试工程师说不会定位元素定位,那么确定也不会作自动化了。css
小伙伴们都知道若是是web端能够经过F12进行查看元素(右击检查查看元素)那么app如何查看呢?app的经过uiautomatorviewer工具进行元素定位,而后获取对应的一些操做。uiautomatorviewer是Android-sdk自带的元素定位工具。android
1.打开uiautomatorviewer工具:android-sdk-windows\tools下web
2.任意点击下图中按钮,获取app页面属相windows
3.经过移动鼠标放到想要定位的位置,而后查看右下角元素属性app
# 格式 find_element_by_id()
点击搜索框,查看右下角元素属性工具
属于id属性,进行点击等操做测试
# 经过ID获取属性 driver.find_element_by_id('com.taobao.taobao:id/home_searchedit').click()
# 格式 find_element_by_class_name()
继续拿上面的图咱们作分析,发现有class=‘android.widget.EditText’ui
# 经过class_name进行定位 find_element_by_class_name('android.widget.EditText')
# 格式 find_element_by_link_text()
上图发现text属性有值为text=‘小米cc9e钢化膜’spa
# 经过text进行定位 find_element_by_link_text('小米cc9e钢化膜')
# 格式 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='属性值']')
# 格式 find_element_by_name() # 这个工具上好像没有name属性,咱们能够在web查看试试
# 格式 find_element_by_tag_name()
# 格式 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属性面前须要加“.”.net
详细的css定位语法见: https://blog.csdn.net/ouyanggengcheng/article/details/77197294
定位方法不在意多少,在意的是如何在最须要的时候用到它(说白了就是,那个方便用那个)
感受今天的知识对您有帮助的话,点个关注,持续更新中~~~~让咱们在知识的海洋中翱翔