一 常用的元素定位方法
二 如何使用元素定位方法
===========================================================================
有哪些常用的元素定位方法?
id定位: find_element_by_id()
name定位: find_element_by_name()
className定位:find_element_by_class_name()
Accessibilityld定位:find_element_by_accessibility_id()
xpath定位:find_element_by_xpath()
======================================================================
如何使用元素定位方法?
前期准备:
1.打开夜神模拟器,adb devices能显式模拟器设备,打开淘宝app
2.使用sdk的tools目录下自带的uiautomatorviewer
![](https://upload-images.jianshu.io/upload_images/16945005-860d8298ec8ecc4e.png?imageMogr2/auto-orient/strip|imageView2/2/w/722)
![](https://upload-images.jianshu.io/upload_images/16945005-82ade502249ae6cb.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200)
id定位
![](https://upload-images.jianshu.io/upload_images/16945005-a862185def74612b.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200)
id定位:是通过元素的resource-id来定位
方法名:find_element_by_id('resource-id值')
name定位:
![](https://upload-images.jianshu.io/upload_images/16945005-52c3da00248a0828.png?imageMogr2/auto-orient/strip|imageView2/2/w/1194)
name定位:是通过元素的text属性
方法名:find_element_by_name('text值')
className定位:
![](https://upload-images.jianshu.io/upload_images/16945005-04de03df36f05d67.png?imageMogr2/auto-orient/strip|imageView2/2/w/1188)
className定位:是通过元素的class来定位
方法名:find_element_by_class_name('class')
accessibilityId定位:
![](https://upload-images.jianshu.io/upload_images/16945005-f2ec42a3e5cfe882.png?imageMogr2/auto-orient/strip|imageView2/2/w/1191)
accessibilityId定位:是通过元素的content-desc来定位
方法名:find_element_by_accessibility_id('content-desc值')
xpath定位:
#示例代码
driver.find_element_by_xpath(“//*[@text='好沙发需要好坐垫']”)
xpath定位:是通过元素的xpath表达式来定位
方法名:find_element_by_xpath('xpath表达式')
xpath的模糊查找
driver.find_element_by_xpath(“//contains[@text,'好沙发需要好坐垫']”)