appium 元素定位find_element_by_android_uiautomator方法使用

若appium中给定的方法无法满足你的需求,刚好uiautomator中的方法可以满足你的需求时,你可使用find_element_by_android_uiautomator来调用uiautomator中的方法来实现。

appium底层文件webdriver中给出的说明如下:

def find_element_by_android_uiautomator(self, uia_string):
        """Finds element by uiautomator in Android.

        :Args:
         - uia_string - The element name in the Android UIAutomator library

        :Usage:
            driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
        """
        return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)

 看了一会,愣是没有明白给出的示例是什么意思,实际脚本中怎么运用,我知道是自己太笨了,所以果断还是找能看懂的示例进行学习吧,因此哈哈还真找到了,将此运用方式写成简单的示例来记录,说明:这里已text为例,其余的uiautomator中的方法使用形式与此一致,因此会一个足以按照此方式来实现其他需求。

示例如下:

# coding=UTF-8
'''
Created on 2017.12.21
@author: Lucky
'''from appium import webdriver

class Customer:

    def __init__(self):
        logging.info("Test_appium.....setUp")  
        desired_cups = {}
        desired_cups['platformName'] = 'Android'
        desired_cups['platformVersion'] = '7.0'
        desired_cups['deviceName'] = 'aa'  
        desired_cups['appPackage']= 'com.ibroker.iBerHK'  
        desired_cups['appActivity'] = '.SplashActivity'  
        self.device = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cups) 
        self.device.implicitly_wait(20)    #全局默认等待最大时间
        
    #第一种 直接点击字符串
  def Enter_Customer_List(self):
'''select:通訊錄導入 and 手動添加''' self.device.find_element_by_android_uiautomator('text("列表")').click() #点击 字符串“列表”

  #第二种 通过参数的给定来操作
  def Enter_Customer_List2(self,name):
        '''select:通訊錄導入 and 手動添加'''
        self.device.find_element_by_android_uiautomator('text("'+name+'")').click()    

if __name__ == "__main__":
  c = Customer()
  c.
Enter_Customer_List()
  c.Enter_Customer_List('列表')

原文地址:https://www.cnblogs.com/syw20170419/p/8082896.html