Appium:获取已定位的元素属性值

本文介绍如何使用Appium和Python在Android APP自动化测试中获取已定位元素的属性值,包括text、size、location、tag_name、content-desc等关键属性。

Appium:获取已定位的元素属性值

手机设置

text

# 已“设置-双卡与网络”为例

loc = driver.find_element_by_xpath("//*[@text='双卡和网络']")

# 获取text属性
print(loc.text)
print(loc.get_attribute("text"))

size & location

loc = driver.find_element_by_xpath("//*[@text='双卡和网络']")

# size, 返回字典:{'height': XXX, 'width': XXX}
print(loc.size)

# location, 返回字典:{'x': XXX, 'y': XXX}
print(loc.location)

tag_name

loc = driver.find_element_by_xpath("//*[@text='双卡和网络']")

# 获取tag_name属性
print(loc.tag_name)

get_attribute

loc = driver.find_element_by_xpath("//*[@text='双卡和网络']")

# resourceId
print(loc.get_attribute("resourceId"))

# className
print(loc.get_attribute("className"))

# 其他属性,如:enabled、checked
print(loc.get_attribute("enabled"))
print(loc.get_attribute("checked"))

content-desc 属性值

# content-desc属性值为空 → 打印:text
loc = driver.find_element_by_xpath('//*[@text="双卡和网络"]')
print(loc.get_attribute('name'))
 
# content-desc属性值不为空 → 打印:content-desc值
search = driver.find_element_by_id("com.android.settings:id/search")	# 右上角搜索按钮
print(search.get_attribute('name'))

参考代码

from appium import webdriver


desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.0'
desired_caps['deviceName'] = '127.0.0.1:62001'	# 夜神模拟器
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = 'com.android.settings.Settings'
desired_caps['noReset'] = "False"

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
import time
time.sleep(5)

loc = driver.find_element_by_xpath('//*[@text="双卡和网络"]')
search = driver.find_element_by_id("com.android.settings:id/search")

print(loc.text)		# 双卡和网络
print(loc.get_attribute("text"))		# 双卡和网络

print(loc.tag_name)		# android.widget.TextView

print(loc.get_attribute("resourceId"))		# com.android.settings:id/title
print(loc.get_attribute("className"))		# android.widget.TextView
print(loc.get_attribute("enabled"))		# true
print(loc.get_attribute("checked"))		# false
print(loc.size)		# {'height': 65, 'width': 240}
print(loc.location)		{'x': 201, 'y': 360}

print(loc.get_attribute('name'))		# 双卡和网络
print(search.get_attribute('name'))		# 搜索

Words

Appium、python + appium、获取元素属性值、APP自动化、UI自动化、测试

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值