Appium appium1.7.1,android7.0+ 上,元素 ID 识别不了

小怪兽 · 2017年11月16日 · 最后由 yichao 回复于 2018年06月11日 · 2494 次阅读

appium 版本 1.7.1,python 环境,使用的 appiumlibrary 库,版本如下:

页面上的元素 “关闭易信首页提示” 在 automator viewer 里面能找到,且在 android5.0 的手机上控件能正常识别

但是在 android7.0、android8.0 的手机上,不管使用 ID 还是 name 定位的方式都找不到。appium 的 log 如下:
[info] [HTTP] <-- POST /wd/hub/session/3623631a-ebf6-4fe2-b4fe-661a9f263b45/element/16/click 200 135 ms - 76
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":true}[info] [HTTP] --> POST /wd/hub/session/3623631a-ebf6-4fe2-b4fe-661a9f263b45/elements {"using":"id","sessionId":"3623631a-ebf6-4fe2-b4fe-661a9f263b45","value":"im.yixin:id/btn_agenda_un_track"}
[debug] [MJSONWP] Calling AppiumDriver.findElements() with args: ["id","im.yixin:id/btn_agenda_un_track","3623631a-ebf6-4fe2-b4fe-661a9f263b45"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator, name
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator, name
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"im.yixin:id/btn_agenda_un_track","context":"","multiple":true}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"im.yixin:id/btn_agenda_un_track","context":"","multiple":true}}
[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 'im.yixin:id/btn_agenda_un_track' using 'ID' with the contextId: '' multiple: true
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=im.yixin:id/btn_agenda_un_track]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=im.yixin:id/btn_agenda_un_track]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=im.yixin:id/btn_agenda_un_track][debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'im.yixin:id/btn_agenda_un_track' using 'ID' with the contextId: '' multiple: true
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=im.yixin:id/btn_agenda_un_track]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=im.yixin:id/btn_agenda_un_track]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=im.yixin:id/btn_agenda_un_track][debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.findElements() result: []
[info] [HTTP] <-- POST /wd/hub/session/3623631a-ebf6-4fe2-b4fe-661a9f263b45/elements 200 9047 ms - 74
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[]}
这个页面的其他元素,在 android7.0、8.0 上能正常识别,只有这个元素,
看了下 appiumlibrary 中 click_element 这个方法,写的是:

def click_element(self, locator):
        """Click element identified by `locator`.

        Key attributes for arbitrary elements are `index` and `name`. See
        `introduction` for details about locating elements.
        """
        self._info("Clicking element '%s'." % locator)
        self._element_find(locator, True, True).click()

求各位指点指点

共收到 4 条回复 时间 点赞

我也遇到了差不多的问题,具体情况是关闭蓝牙,进入 app 后会有一个提示框(提示是否开启蓝牙),我点击不开启,等待提示框小时候点击页面上的菜单按钮,可是发现无论如何也点击不了菜单按钮,最后也报错说找不到菜单按钮(菜单按钮我用的是 ID 进行定位的),最后我发现在 7.0 的手机上就不行,在 7.0 以下的手机就可以运行成功,而且我要是将点击提示框的那段代码注销掉,然后在实际执行中开启蓝牙,不让提示框出现,剩余的代码可以完美的执行,研究了半天也没有发现为何会这样!

ele=driver.find_element_by_xpath("//*[@text='关闭易信首页提示']").click(),你用 xpath 这样定位试试看看

automationName 换 UiAutomation2,默认的 Appium 已经不建议使用了,使用的话会出现某些 by_id 无法定位,by_name 在 NATIVE APP 中已经不用了。

遇到同样问题,使用 UiAutomation2 方式解决

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册