Appium appium 这个 id 为什么取不到呢

skyshine · 2019年02月10日 · 最后由 hellohui123 回复于 2019年02月17日 · 594 次阅读

DY的视频下面,在歌名上都会有几句说明。

我用appium。取文字的时候,总是找不到,是为什么?

find_element_by_id('com.ss.android.ugc.aweme:id/l7')

错误

selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

试了好多次,查了几遍id没错。但就是不行。
奇怪

共收到 21 条回复 时间 点赞

id并不是resource-id

2楼 已删除
陈恒捷 回复

命令行下取dump .xml没问题,播放和暂停都可以。
虽然有时也会取不到。

但在我在python里执行命令行,就永远取不到了。

我看了你说的暂停那个,但在代码里总是不行。搜了几天也没有提到解决办法的,哎

陈恒捷 回复

用你图里的工具和uiautomatorviewer是都能找到元素的。
但是写在代码 里跑就找不到了。。

我试你说的暂停那个先。

=============================================
暂停视频也是不行

我试过命令行下用adb里用命令取xml
不管视频播放停止都可以。。

而且还能找到我要的那个元素

董延京 回复

模拟器不息,
延迟几秒都没用

我用 appium desktop 试了下,貌似是可以找到的哦。

appium desktop 版本:1.8.2.20181101.4
手机 android 版本:5.1
抖音安装包:大概2天前官方下载的,文件名: app_aweGW_v4.3.3_84a2a6f.apk

搜索日志:

[HTTP] --> POST /wd/hub/session/3ac01ec6-d048-4e5d-ac0b-cfd64ba010eb/elements
[HTTP] {"using":"id","value":"com.ss.android.ugc.aweme:id/l7"}
[MJSONWP] Calling AppiumDriver.findElements() with args: ["id","com.ss.android.ugc.aweme:id/l7","3ac01ec6-d048-4e5d-ac0b-cfd64ba010eb"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 0 ms for condition
[AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.ss.android.ugc.aweme:id/l7","context":"","multiple":true}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.ss.android.ugc.aweme:id/l7","context":"","multiple":true}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.ss.android.ugc.aweme:id/l7' using 'ID' with the contextId: '' multiple: true
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (1)
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=1, RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[{"ELEMENT":"5"}]}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.findElements() result: [{"ELEMENT":"5"}]
[HTTP] <-- POST /wd/hub/session/3ac01ec6-d048-4e5d-ac0b-cfd64ba010eb/elements 200 194 ms - 89

有一个点要特别留意,如果视频在播放,此时页面处于动态状态,控件树是不会 dump 出来的。得暂停了视频后才能比较快地 dump 出来。具体原理可以百度下 uiautomator dump 控件树原理相关的文章。

有试过对比 page_source 获取到的 xml 树,和 uiautomatorviewer 相比,缺少了哪些元素吗?

按以前经验,确实会有部分元素 viewer 可见,page_source 不可见,例如 webview 里面的元素。但从你的 viewer 截图上看,这个控件及它的上层所属的类应该都是原生布局,不大像是 webview 里面的元素。

skyshine 回复

1分钟多。。。咋这么久啊。。。不是几秒就好了吗,1分多钟手机不熄屏吗

董延京 回复

time.sleep(70)

陈恒捷 回复
[HTTP] 
[HTTP] --> GET /wd/hub/session/b9847749-158b-4de8-9dd6-67017f1e1a70/source
[HTTP] {}
[MJSONWP] Calling AppiumDriver.getPageSource() with args: ["b9847749-158b-4de8-9dd6-67017f1e1a70"]
[AndroidBootstrap] Sending command to android: {"cmd":"action","action":"source","params":{}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"source","params":{}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: source
[AndroidBootstrap] [UIAUTO STDOUT] [APPIUM-UIAUTO] [debug] Returning result: {"status":0,"value":"
[AndroidBootstrap] [UIAUTO STDOUT] crollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,1208][720,1280]\" resource-id=\"com.ss.android.ugc.aweme:id\/na\" instance=\"2\"><\/android.widget.LinearLayout><\/android.widget.LinearLayout><\/android.widget.LinearLayout><\/android.widget.LinearLayout><\/android.widget.FrameLayout><\/android.wid
[AndroidBootstrap] Received command result from bootstrap
[AndroidBootstrap] Stream still not complete, waiting up to 60000ms for the data to arrive
[AndroidBootstrap] [UIAUTO STDOUT] get.LinearLayout><\/android.widget.FrameLayout><\/hierarchy>"
}[/APPIUM-UIAUTO]

[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.getPageSource() result: "
[HTTP] <-- GET /wd/hub/session/b9847749-158b-4de8-9dd6-67017f1e1a70/source 200 58 ms - 4151
[HTTP]

page_source 的日志,你说的是这一段吗?


就是红框里面那句

11楼 已删除
skyshine 回复

这个是 find_element 的日志,不是 page_source 的日志吧?

另外,实际界面上,这个元素真的有出现了吗?能否也截个图看看?

找元素前加等待时间了吗,一般找不到元素是这个原因~

陈恒捷 回复
[HTTP] --> GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time
[HTTP] {}
[MJSONWP] Calling AppiumDriver.getDeviceTime() with args: [null,"2a83c17d-a7bf-4fe6-affa-4e84468f0ceb"]
[AndroidDriver] Attempting to capture android device date and time
[ADB] Running 'E:\SDk\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62027 shell date +%Y-%m-%dT%T%z'
[MJSONWP] Responding to client with driver.getDeviceTime() result: "2019-02-12T23:05:40+08:00"
[HTTP] <-- GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time 200 109 ms - 99
[HTTP]
[HTTP] --> POST /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/element
[HTTP] {"using":"id","value":"com.ss.android.ugc.aweme:id/l7","sessionId":"2a83c17d-a7bf-4fe6-affa-4e84468f0ceb"}
[MJSONWP] Calling AppiumDriver.findElement() with args: ["id","com.ss.android.ugc.aweme:id/l7","2a83c17d-a7bf-4fe6-affa-4e84468f0ceb"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 0 ms for condition
[AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.ss.android.ugc.aweme:id/l7","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.ss.android.ugc.aweme:id/l7","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.ss.android.ugc.aweme:id/l7' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.ss.android.ugc.aweme:id/l7' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.ss.android.ugc.aweme:id/l7]
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Matched JSONWP error code 7 to NoSuchElementError
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[MJSONWP] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.
[MJSONWP] at AndroidDriver.callee$0$0$ (C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\lib\commands\find.js:75:11)
[MJSONWP] at tryCatch (C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
[MJSONWP] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
[MJSONWP] at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
[MJSONWP] at GeneratorFunctionPrototype.invoke (C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[MJSONWP] at run (C:\Program Files (x86)\Appium\resources\app\webpack:\~\babel-polyfill\~\core-js\modules\es6.promise.js:75:1)
[MJSONWP] at C:\Program Files (x86)\Appium\resources\app\webpack:\~\babel-polyfill\~\core-js\modules\es6.promise.js:92:1
[MJSONWP] at flush (C:\Program Files (x86)\Appium\resources\app\webpack:\~\babel-polyfill\~\core-js\modules\_microtask.js:18:1)
[MJSONWP] at process._tickCallback (internal/process/next_tick.js:61:11)
[HTTP] <-- POST /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/element 500 962 ms - 164
[HTTP]
[HTTP] --> GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time
[HTTP] {}
[MJSONWP] Calling AppiumDriver.getDeviceTime() with args: [null,"2a83c17d-a7bf-4fe6-affa-4e84468f0ceb"]
[AndroidDriver] Attempting to capture android device date and time
[ADB] Running 'E:\SDk\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62027 shell date +%Y-%m-%dT%T%z'
[MJSONWP] Responding to client with driver.getDeviceTime() result: "2019-02-12T23:07:35+08:00"
[HTTP] <-- GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time 200 102 ms - 99
[HTTP]
[HTTP] --> GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time
[HTTP] {}
[MJSONWP] Calling AppiumDriver.getDeviceTime() with args: [null,"2a83c17d-a7bf-4fe6-affa-4e84468f0ceb"]
[AndroidDriver] Attempting to capture android device date and time
[ADB] Running 'E:\SDk\platform-tools\adb.exe -P 5037 -s 127.0.0.1\:62027 shell date +%Y-%m-%dT%T%z'
[MJSONWP] Responding to client with driver.getDeviceTime() result: "2019-02-12T23:07:35+08:00"
[HTTP] <-- GET /wd/hub/session/2a83c17d-a7bf-4fe6-affa-4e84468f0ceb/appium/device/system_time 200 103 ms - 99
[HTTP]

这好像也只是提示没找到元素。。。
我没太看出别的异常。我的分析对吗?

skyshine 回复

page_source 时,可以同时也留意下 appium 的日志,看有没有什么异常。

chen 回复

试过等待,昨晚发现page_source没有这个元素。
找别的元素的时候,page_source里面都有。到这个就没了。
不明白怎么回事?

陈恒捷 回复

嗯。昨天晚上试过,page_source也没有这个元素
然后搜了半天,也没找到什么办法

建议在 find 函数加个断点,然后在断点的时候通过 page_source 获取下界面的控件树,看是不是控件树和 uiautomatorviewer 看到的有出入。

噢,抖音不存在我说的那个动态js的问题,你获取id的方式也是对的,为什么会报找不到元素,你在find元素之间有wait的过程吗?做了隐式等待 显示等待 强制等待其中一个么?

chen 回复

就是douyin,刚好学习,就用这个挺火的练习

你说的内嵌js 或 动态获取,要用什么方法确定?

陈恒捷 回复


就是这个(刚好截时是个美女图,涂掉吧)
红框那个

不是很清楚这个DY视频是啥?但是一般取不到id的原因,我知道一些,可以分享一下,帮你解决问题。
首先先确认一下这个app是不是内嵌js的?如果是,再确认一下是不是动态获取js的?如果是,得用个driver来启动他。。。(但我看着你这个获取id的方式并不是hybrid或web这种的),貌似是Native的
那你在看看这个描述,是不是一开始就能在屏幕内出现,还是说需要滑动一下,才能出现?如果是,你需要加一个滑动事件,让这个描述出现在手机屏幕上;

你的 id 是怎么找到的,uiautomatorviewer 截图发下?

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