Appium 请教元素概率性失败的原因

酷火 · 2015年06月26日 · 最后由 陈恒捷 回复于 2015年06月27日 · 2781 次阅读

查找某个元素,实际该元素已经出现,但大约 20% 是失败的,请教原因~

driver.startActivity("com.autonavi.xmgd.navigator", ".Warn");   //直接开启应用

WebDriverWait wait = new WebDriverWait(driver,10); 
wait.until(new ExpectedCondition<WebElement>(){   
    @Override  
    public WebElement apply(WebDriver d) {   
       return d.findElement(By.id("com.autonavi.xmgd.navigator:id/map_maplayerdrawer"));
    }}); 

Appium 的 log

> info: --> POST /wd/hub/session {"desiredCapabilities":{"appPackage":"com.autonavi.xmgd.navigator","appActivity":".Warn","noReset":"true","newCommandTimeout":400,"platformVersion":"4.4.4","automationName":"appium","browserName":"","unicodeKeyboard":"True","platformName":"Android","deviceName":"71MBBLH22TYJ","resetKeyboard":"True"}}
> info: Client User-Agent string: Apache-HttpClient/4.3.4 (java 1.5)
> warn: Converting cap noReset from string to boolean. This might cause unexpected behavior.
> warn: Converting cap unicodeKeyboard from string to boolean. This might cause unexpected behavior.
> warn: Converting cap resetKeyboard from string to boolean. This might cause unexpected behavior.
> info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device
> info: [debug] Creating new appium session 8f796041-234a-46c7-804f-92b1a71f3e3b
> info: Starting android appium
> info: [debug] Getting Java version
> info: Java version is: 1.8.0_45
> info: [debug] Checking whether adb is present
> info: [debug] Using adb from D:\android-sdk-windows\platform-tools\adb.exe
> warn: No app capability, can't parse package/activity
> info: [debug] Set chromedriver binary as: C:\Program Files\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> info: [debug] Using fast reset? false
> info: [debug] Preparing device for session
> info: [debug] Not checking whether app is present since we are assuming it's already on the device
> info: Retrieving device
> info: [debug] Trying to find a connected android device
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe devices
> info: [debug] 1 device(s) connected
> info: Found device 71MBBLH22TYJ
> info: [debug] Setting device id to 71MBBLH22TYJ
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ wait-for-device
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "echo 'ready'"
> info: [debug] Starting logcat capture
> info: [debug] Getting device API level
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 19
> info: Device API level is: 19
> info: [debug] Extracting strings for language: default
> info: [debug] Apk doesn't exist locally
> info: [debug] Could not get strings, but it looks like we had an old strings file anyway, so ignoring
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "rm -rf /data/local/tmp/strings.json"
> info: [debug] Not uninstalling app since server not started with --full-reset
> info: [debug] Skipping install since we launched with a package instead of an app path
> info: [debug] Forwarding system:4724 to device:4724
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ forward tcp:4724 tcp:4724
> info: [debug] Pushing appium bootstrap to device...
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ push "C:\\Program Files\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: [debug] Enabling Unicode keyboard support
> info: [debug] Pushing unicode ime to device...
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ install "C:\Program Files\Appium\node_modules\appium\build\unicode_ime_apk\UnicodeIME-debug.apk"
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "settings get secure default_input_method"
> info: [debug] Unsetting IME 'com.baidu.input_mz/com.meizu.input.MzInputService'
> info: [debug] Setting IME to 'io.appium.android.ime/.UnicodeIME'
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "ime enable io.appium.android.ime/.UnicodeIME"
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "ime set io.appium.android.ime/.UnicodeIME"
> info: [debug] Pushing settings apk to device...
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ install "C:\Program Files\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ install "C:\Program Files\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: Starting App
> info: [debug] Attempting to kill all 'uiautomator' processes
> info: [debug] Getting all processes with 'uiautomator'
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.autonavi.xmgd.navigator -e disableAndroidWatchers false
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
> info: [debug] [UIAUTOMATOR STDOUT] tcp port:4724
> info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> info: [debug] Waking up device if it's not alive
> info: [debug] Pushing command to appium work queue: ["wake",{}]
> info: [debug] [BOOTSTRAP] [debug] Client connected
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"wake","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: wake
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "dumpsys window"
> info: [debug] Screen already unlocked, continuing.
> info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"\/data\/local\/tmp","status":0}
> info: [debug] dataDir set to: /data/local/tmp
> info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"compressedLayoutHierarchy","params":{"compressLayout":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
> info: [debug] Getting device API level
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 19
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.autonavi.xmgd.navigator/.Warn"
> info: [debug] Waiting for pkg "com.autonavi.xmgd.navigator" and activity ".Warn" to be focused
> info: [debug] Getting focused package and activity
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "dumpsys window windows"
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "getprop ro.build.version.release"
> info: [debug] Device is at release version 4.4.4
> info: [debug] Device launched! Ready for commands
> info: [debug] Setting command timeout to 400 secs
> info: [debug] Appium session started with sessionId 8f796041-234a-46c7-804f-92b1a71f3e3b
> info: <-- POST /wd/hub/session 303 8200.051 ms - 74 
> info: --> GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b {}
> info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"4.4.4","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"appPackage":"com.autonavi.xmgd.navigator","appActivity":".Warn","noReset":true,"newCommandTimeout":400,"platformVersion":"4.4.4","automationName":"appium","browserName":"","unicodeKeyboard":true,"platformName":"Android","deviceName":"71MBBLH22TYJ","resetKeyboard":true},"appPackage":"com.autonavi.xmgd.navigator","appActivity":".Warn","noReset":true,"newCommandTimeout":400,"automationName":"appium","unicodeKeyboard":true,"platformName":"Android","deviceName":"71MBBLH22TYJ","resetKeyboard":true},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: <-- GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b 200 1.050 ms - 819 {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"4.4.4","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"appPackage":"com.autonavi.xmgd.navigator","appActivity":".Warn","noReset":true,"newCommandTimeout":400,"platformVersion":"4.4.4","automationName":"appium","browserName":"","unicodeKeyboard":true,"platformName":"Android","deviceName":"71MBBLH22TYJ","resetKeyboard":true},"appPackage":"com.autonavi.xmgd.navigator","appActivity":".Warn","noReset":true,"newCommandTimeout":400,"automationName":"appium","unicodeKeyboard":true,"platformName":"Android","deviceName":"71MBBLH22TYJ","resetKeyboard":true},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: --> GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/window/current/size {}
> info: [debug] Pushing command to appium work queue: ["getDeviceSize"]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDeviceSize","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDeviceSize
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"width":1080,"height":1920},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"width":1080,"height":1920},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: <-- GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/window/current/size 200 9.691 ms - 100 {"status":0,"value":{"width":1080,"height":1920},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: --> GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/window/current/size {}
> info: [debug] Pushing command to appium work queue: ["getDeviceSize"]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDeviceSize","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDeviceSize
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"width":1080,"height":1920},"status":0}
> info: [debug] Responding to client with success: {"status":0,"value":{"width":1080,"height":1920},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: <-- GET /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/window/current/size 200 6.754 ms - 100 {"status":0,"value":{"width":1080,"height":1920},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: --> POST /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/element {"using":"id","value":"com.autonavi.xmgd.navigator:id/map_maplayerdrawer"}
> info: [debug] Waiting up to 0ms for condition
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.autonavi.xmgd.navigator:id/map_maplayerdrawer","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.autonavi.xmgd.navigator:id/map_maplayerdrawer","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding com.autonavi.xmgd.navigator:id/map_maplayerdrawer using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.autonavi.xmgd.navigator:id/map_maplayerdrawer]
> info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
> info: [debug] [BOOTSTRAP] [debug] Finding com.autonavi.xmgd.navigator:id/map_maplayerdrawer using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.autonavi.xmgd.navigator:id/map_maplayerdrawer]
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"No element found","status":7}
> info: [debug] Condition unmet after 11532ms. Timing out.
> info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: <-- POST /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b/element 500 11532.573 ms - 195 
> info: --> DELETE /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b {}
> info: Shutting down appium session
> info: [debug] Pressing the HOME button
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "input keyevent 3"
> info: [debug] Resetting IME to 'com.baidu.input_mz/com.meizu.input.MzInputService'
> info: [debug] executing cmd: D:\android-sdk-windows\platform-tools\adb.exe -s 71MBBLH22TYJ shell "ime set com.baidu.input_mz/com.meizu.input.MzInputService"
> info: [debug] Stopping logcat capture
> info: [debug] Logcat terminated with code null, signal SIGTERM
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
> info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
> info: [debug] [UIAUTOMATOR STDOUT] [CDS]close[4724]
> info: [debug] [UIAUTOMATOR STDOUT] close [socket][/0.0.0.0:4724]
> info: [debug] [BOOTSTRAP] [debug] Closed client connection
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
> info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
> info: [debug] [UIAUTOMATOR STDOUT] Time: 14.816
> info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
> info: [debug] UiAutomator shut down normally
> info: [debug] Cleaning up android objects
> info: [debug] Cleaning up appium session
> info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}
> info: <-- DELETE /wd/hub/session/8f796041-234a-46c7-804f-92b1a71f3e3b 200 1661.672 ms - 76 {"status":0,"value":null,"sessionId":"8f796041-234a-46c7-804f-92b1a71f3e3b"}


共收到 4 条回复 时间 点赞
info: [debug] Waiting up to 0ms for condition

看来你的 wait 没有生效,只找了一次就返回了。
你看下你的 wait 写法有没有问题?

似乎是没找到超时了. 看看适当延长 wait 的时间, 或者在 activity 开启之后加个 sleep 看看是否能稳定查找到.

1、规范来说,需要改写 waituntil 方法,具体百度吧。
2、另外,你的 driver 也没有初始化后进行参数配置,你百度下隐式等待。
3、你这个标题党。。。原因嘞?。。。

@chenhengjie123 感觉 wait 是有些问题,但还不知道怎么改
@tbya 延长时间没有用,那个元素也只会显示 5s,5s 后就不显示了
@pighero001
1、如你所说,确实要修改,还不知道怎么修改
2、隐式等待也是概率性可以
3、我改下标题吧

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