目录
文中涉及到定位元素->
参考链接:如何定位元素?封装查找元素方法
一个具体场景:
某个元素属性 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()