问题:
使用 https://testerhome.com/topics/11720 的框架,通过 uiautomator2 识别元素,失败。
经过简单的查看日志,在页面更新后,需要刷新页面缓存才对,实际上切换 uiautomator2 框架没有这个操作。
操作
工程代码: https://github.com/leila-wu/pythonAppium 。执行 Runner 下的 runner.py,调用 HomeTest.py 执行用例。
日志与描述:
切换 desired_caps['automationName'] = 'Appium'。识别元素成功。 重点看到Failed to locate element. Clearing Accessibility cache and retrying.。取值失败,有清除缓存处理
该场景的其他元素获取正常,需要重新识别的元素,是页面小范围刷新。比如点击 A 按钮新增的 B 展示后,元素 B 获取失败
[debug] [W3C] Calling AppiumDriver.findElement() with args: ["id","com.thinkive.future.dev.standard:id/tv_optional_edit","6757fcaa-b7b8-4ccf-8486-2d538f86d617"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.thinkive.future.dev.standard:id/tv_optional_edit' using 'ID' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.thinkive.future.dev.standard:id/tv_optional_edit]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.thinkive.future.dev.standard:id/tv_optional_edit' using 'ID' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.thinkive.future.dev.standard:id/tv_optional_edit]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"ELEMENT":"13"}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [W3C] Responding to client with driver.findElement() result: {"ELEMENT":"13"}
[HTTP] <-- POST /wd/hub/session/6757fcaa-b7b8-4ccf-8486-2d538f86d617/element 200 214 ms - 88
切换 desired_caps['automationName'] = 'uiautomator2'。元素识别失败(端口随机处理。)
[debug] [W3C] Calling AppiumDriver.findElement() with args: ["id","com.thinkive.future.dev.standard:id/tv_optional_edit","8c510efb-4426-45bc-84eb-971441239f1e"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:4754/wd/hub/session/b6d92bbf-10c9-475b-b382-ffb975645773/element] with body: {"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}
[HTTP] <-- POST /wd/hub/session/8c510efb-4426-45bc-84eb-971441239f1e/element 404 28 ms - 1119
[HTTP]
检查代码
WebDriverWait(self.driver, t).until(lambda x: self.elements_by(mOperate)) # 操作元素是否存在
# 封装常用的标签
def elements_by(self, mOperate):
elements = {
BE.find_element_by_id: lambda: self.driver.find_element_by_id(mOperate["element_info"]),
BE.find_element_by_xpath: lambda: self.driver.find_element_by_xpath(mOperate["element_info"]),
BE.find_element_by_css_selector: lambda: self.driver.find_element_by_css_selector(mOperate['element_info']),
BE.find_element_by_class_name: lambda: self.driver.find_element_by_class_name(mOperate['element_info']),
BE.find_elements_by_id: lambda: self.driver.find_elements_by_id(mOperate['element_info']),
}
return elements[mOperate["find_type"]]()
脚本配置:
- element_info: com.thinkive.future.dev.standard:id/tv_optional_edit
find_type: id
operate_type: click
info: 点击编辑按钮