Appium + Python -------------元素定位

說在前面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")

 

--------若是上面說的有什麼問題,麻煩你們及時扣扣我!感恩!

--------會不定時更新,轉載請說明原文

相關文章
相關標籤/搜索