【Appium】如何使用 content-desc 定位元素?(4种方法)

文中涉及到定位元素->
参考链接:如何定位元素?封装查找元素方法

一个具体场景:
某个元素属性 content-desc = “option_selected_icon”

一、方法1 - 使用 ACCESSIBILITY_ID 定位

(和下面的方法2其实一样,但是这里封装了下->推荐这样使用!!!

from appium.webdriver.common.mobileby import MobileBy
_selected_icon2 = (MobileBy.ACCESSIBILITY_ID, 'option_selected_icon')
self.find(self._selected_icon2).click()

二、方法2 - 使用 find_element_by_accessibility_id 定位

self.driver.find_element_by_accessibility_id('option_selected_icon').click()

三、方法3 - 使用 find_element_by_android_uiautomator 定位

self.driver.find_element_by_android_uiautomator('new UiSelector().description("option_selected_icon")').click()

四、方法4 - 使用 xpath 定位

_selected_icon = (By.XPATH,'//*[@content-desc="option_selected_icon"]')
self.find(self._selected_icon).click()

说明: # 以下两种写法么有区别,’'和""

_selected_icon = (By.XPATH,'//*[@content-desc="option_selected_icon"]')
或
_selected_icon = (By.XPATH, "//*[@content-desc='option_selected_icon']")

五、其他情况

如果这个 content-desc = “option_selected_icon” 对应多个元素?

可以这样写:
参考链接:如何定位元素?封装查找元素方法

from appium.webdriver.common.mobileby import MobileBy
_selected_icon = (By.XPATH,'//*[@content-desc="option_selected_icon"]')
# 定位第二个匹配的元素,点它
self.findAll(self._selected_icon)[1].click()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值