Appium+python自動化28-獲取元素屬性get_attribute

獲取text

# coding:utf-8 from appium import webdriver from time import sleep desired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity', 'noReset': 'true', 'resetKeyboard': 'true', 'unicodeKeyboard': 'true' } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 等主頁面activity出現 driver.wait_activity(".base.ui.MainActivity", 10) # 點取消升級 driver.find_element_by_id("com.baidu.yuedu:id/negativeUpgrade").click() # 獲取text t1 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").text print(t1)

tag_name

1.tag_name實質上是獲取class屬性python

# 獲取tag_name t2 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").tag_name print(t2) 

2.打印結果:android.widget.TextViewandroid

get_attribute

1.獲取content-desc屬性,這裏注意了,若是content-desc屬性爲空,那麼獲取的就是text屬性,不爲空獲取的纔是content-desc屬性web

2.content-desc屬性爲空,打印結果:書架app

# content-desc爲空,獲取的是text t3 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("name") print(t3)

3.content-desc屬性不爲空,打印結果:百度閱讀ui

# content-desc t4 = driver.find_element_by_id("com.baidu.yuedu:id/fragment_banner").get_attribute("name") print t4 

4.id,calss,text屬性獲取spa

# id t5 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("resourceId") print(t5) # class t6 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("className") print(t6) # text t7 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("text") print(t7)

5.其它屬性獲取,注意這裏並非全部的均可以獲取,一些標準的屬性是能夠獲取到的3d

# checkable t8 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("checkable") print t8 # clickable t9 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("clickable") print t9 

size和location

1.獲取size,返回的是字典,如:{'width': 84, 'height': 84}code

2.獲取location,返回的是字典,如:{'y': 38, 'x': 192}orm

# size t10 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").size print t10 # location t11 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").location print t11 

參考代碼

# coding:utf-8 from appium import webdriver desired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity', 'noReset': 'true', 'resetKeyboard': 'true', 'unicodeKeyboard': 'true' } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 等主頁面activity出現 driver.wait_activity(".base.ui.MainActivity", 10) # 點取消升級 driver.find_element_by_id("com.baidu.yuedu:id/negativeUpgrade").click() # 獲取text t1 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").text print(t1) # 獲取tag_name t2 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").tag_name print(t2) # content-desc爲空,獲取的是text t3 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("name") print(t3) # content-desc t4 = driver.find_element_by_id("com.baidu.yuedu:id/fragment_banner").get_attribute("name") print t4 # id t5 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("resourceId") print(t5) # class t6 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("className") print(t6) # text t7 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("text") print(t7) # checkable t8 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("checkable") print t8 # clickable t9 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("clickable") print t9 # size t10 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").size print t10 # location t11 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").location print t11

相關文章
相關標籤/搜索