说在前面python
一、https://github.com/appium/python-client/tree/master/test 里面有一些test ,能够看看,研究研究android
二、学会使用 uiautomatorviewer 和 hierarchyviewer ,这两个工具能够帮助查看app一些信息,很是好用,在android-tools下git
控件的特征就是控件的属性,因此咱们能够根据控件的特征进行定位github
PS:断言部分以后再细说正则表达式
一、find_elements_by_accessibility_id,以accessibility_id进行定位,对Android而言,就是content-description属性(使用uiautomatorviewer 能够查看到) ,因此参数不要弄错app
el = self.driver.find_element_by_accessibility_id(u'请输入QQ号码或手机或邮箱') #以QQ登陆页为例 self.assertIsNotNone(el) els = self.driver.find_elements_by_accessibility_id('请输入QQ号码或手机或邮箱') self.assertIsInstance(els, list)
二、find_element_by_class_name,根据class进行定位工具
self.driver.find_element_by_class_name("android.widget.EditText") # 定位惟一元素 self.driver.find_elements_by_class_name("android.widget.EditText")[0] # 找到全部android.widget.EditText并定位第一个
三、find_elemnt_by_name ,根据name进行定位,对于android来讲,就是text属性ui
e3 = self.driver.find_element_by_name(u"登 录")
四、find_element_by_android_uiautomator ,使用uiautomator定位,后面参数更改便可spa
UiSelector().text 根据text属性进行定位code
self.driver.find_element_by_android_uiautomator('new UiSelector().text("33001122")')
UISelector.textContains 根据text属性模糊定位
e6 = self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("1122")')
UISelector.textStartsWith 根据text的前面几位是否与text一致来定位
e7 = self.driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("33")')
UISelector.textMatches 经过正则表达式和text来进行定位,正则不怎么会,先不扩展了
UISelector.className 经过class来进行定位,合理利用层级定位,例如找到全部的Edittext而后根据text定位
e8 = self.driver.find_element_by_android_uiautomator('new UiSelector()' '.className("android.widget.EditText")' '.textContains("33")')
UISelector.classNameMatches 经过正则表达式和class来进行定位
还有UiSelector.descriptionMatches 、 UiSelector.descriptionStartWith 、UiSelector.description
五、driver.find_element_by_id 与 UiSeletor.resourceId 都是经过resourceId 来进行定位,这个属性只有在Api Level18以上才有
e9 = self.driver.find_element_by_android_uiautomator('new Uiseletor()' '.resourceId' '("com.taobao.qianniu:id/accountCompleteTextView")') e10 = self.driver.find_element_by_id("com.taobao.qianniu:id/accountCompleteTextView")
--------若是上面说的有什么问题,麻烦你们及时扣扣我!感恩!
--------会不定时更新,转载请说明原文