APP元素常用定位方式

1、常用的定位方式

name value
id id属性值
class class属性值
xpath xpath表达式

        

      

  

 

 

2、通过id定位

  方法:driver.find_element_by_id(id_value)  #id_value为id属性值

3、通过class定位

  方法:driver.find_element_by_class_name(class_value)    #class_value为class属性值

4、通过xpath定位

  方法:driver.find_element_by_xpath(xpath_value)      #xpath_value为xpath表达值

  

  xptah常用属性定位:
      1. id ://*[contains(@resource-id,'com.android.settings:id/search')] 
      2. class ://*[contains(@class,'android.widget.ImageButton')]
      3. text ://*[contains(@text,'WLA')]

  模糊定位 contains(@key,value): value可以是部分值

5、定位一组元素

  只需把element改为elements即可

6、其他定位方式:android_uiautomator定位

  方法1:driver.find_element_by_android_uiautomator('new UiSelector().resourceId("id属性值")')

  方法2:driver.find_element_by_android_uiautomator('new UiSelector().text("text文本")')

  方法3:driver.find_element_by_android_uiautomator('new UiSelector().containstext("包含text文本")')

  方法4:driver.find_element_by_android_uiautomator('new UiSelector().ClassName("class属性值")')

  方法5:driver.find_element_by_android_uiautomator('new UiSelector().description("content-des属性值")')

  方法6:driver.find_element_by_android_uiautomator('new UiSelector().starwith("以txt文本开头")')

7、案例  定位设置里面的蓝牙

from appium import webdriver
import time
desired_caps = {
"platformName":"Android",
"platformVersion":'5.1.1',
"deviceName":"127.0.0.1:21503",
"appPackage":"com.android.settings",
"appActivity":".Settings",
"unicodeKeyboard":True,
"resetKeyboard":True
}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(3)
#id定位
#driver.find_elements_by_id('com.android.settings:id/title')[1].click()
#class定位
#driver.find_elements_by_class_name('android.widget.TextView')[4].click()
#xpath定位
#driver.find_element_by_xpath("//*[@text='蓝牙']").click()
#android_automator定位
#driver.find_element_by_android_uiautomator('new UiSelector().text("蓝牙")').click()
#driver.quit()

  



原文地址:https://www.cnblogs.com/xwxxh/p/12660183.html