1. 程式人生 > 其它 >28、根據屬性名獲取屬性值

28、根據屬性名獲取屬性值

  • 應用場景
    • 根據特徵定位到元素後,使元素的屬性名獲取對應的屬性值
  • 方法名
# 對element進行點選操作
# 引數:
#	value:要獲取的屬性名
# 返回值:
#	根據屬性名得到的屬性值
element.get_attribute(value)  # value:元素的屬性
  • 示例
    • 開啟設定
    • 獲取所有resource-id為"com.android.settings:id/title"的元素
    • 使用get_attribute獲取這些元素的enable、text、content-desc、resource-id、class的屬性值
  • 核心程式碼:
titles = driver.find_elements_by_id('android:id/title')
print(titles)
for i in titles:
    print(i.get_attribute("text"))
    print(i.get_attribute('resource-id'))
    print(i.get_attribute('enabled'))
    print(i.get_attribute('name'))
    print(i.get_attribute('ClassName'))
  • 注意點:
    • value="text":返回text的屬性值
    • value="name":返回content-desc/text屬性值
    • value="className":返回class屬性值,只有API>=18才能支援
    • value="resourceId":返回resource-id屬性值,只有API>=18才能支援