最近在使用 appium+Python 进行微信小程序的自动化测试,有些页面无法定位到元素,使用 driver.contexts 获取页面的 webview 也没有获取

def switch_h5():
    text=driver.contexts
    print text

> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "settings get secure default_input_method"
> info: [debug] Unsetting IME 'com.baidu.input_huawei/.ImeService'
> info: [debug] Setting IME to 'io.appium.android.ime/.UnicodeIME'
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "ime enable io.appium.android.ime/.UnicodeIME"
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "ime set io.appium.android.ime/.UnicodeIME"
> info: [debug] Pushing settings apk to device...
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 install "E:\tools\App_tools\Appium\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 install "E:\tools\App_tools\Appium\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: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.tencent.mm -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] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] Waking up device if it's not alive
> info: [debug] Pushing command to appium work queue: ["wake",{}]
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> 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: {"status":0,"value":true}
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 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: {"status":0,"value":"\/data\/local\/tmp"}
> 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: {"status":0,"value":false}
> info: [debug] Getting device API level
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 22
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.tencent.mm/com.tencent.mm.ui.LauncherUI"
> info: [debug] Waiting for pkg "com.tencent.mm" and activity "com.tencent.mm.ui.LauncherUI" to be focused
> info: [debug] Getting focused package and activity
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "dumpsys window windows"
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "getprop ro.build.version.release"
> info: [debug] Device is at release version 5.1
> info: [debug] Device launched! Ready for commands
> info: [debug] Setting command timeout to the default of 60 secs
> info: [debug] Appium session started with sessionId b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf
> info: <-- POST /wd/hub/session 303 11498.305 ms - 74 
> info: --> GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf {}
> info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"PJQDU16715003110","unicodeKeyboard":true,"androidWaitPackage":"com.tencent.mm","noReset":true,"resetKeyboard":true,"platformVersion":"5.1","appPackage":"com.tencent.mm","platformName":"Android","appActivity":"com.tencent.mm.ui.LauncherUI"},"deviceName":"PJQDU16715003110","unicodeKeyboard":true,"androidWaitPackage":"com.tencent.mm","noReset":true,"resetKeyboard":true,"appPackage":"com.tencent.mm","platformName":"Android","appActivity":"com.tencent.mm.ui.LauncherUI"},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf 200 0.735 ms - 809 {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"deviceName":"PJQDU16715003110","unicodeKeyboard":true,"androidWaitPackage":"com.tencent.mm","noReset":true,"resetKeyboard":true,"platformVersion":"5.1","appPackage":"com.tencent.mm","platformName":"Android","appActivity":"com.tencent.mm.ui.LauncherUI"},"deviceName":"PJQDU16715003110","unicodeKeyboard":true,"androidWaitPackage":"com.tencent.mm","noReset":true,"resetKeyboard":true,"appPackage":"com.tencent.mm","platformName":"Android","appActivity":"com.tencent.mm.ui.LauncherUI"},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/timeouts/implicit_wait {"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf","ms":30000}
> info: [debug] Set Android implicit wait to 30000ms
> info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/timeouts/implicit_wait 200 0.965 ms - 76 {"status":0,"value":null,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/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: {"status":0,"value":{"height":1776,"width":1080}}
> info: [debug] Responding to client with success: {"status":0,"value":{"height":1776,"width":1080},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/window/current/size 200 7.619 ms - 100 {"status":0,"value":{"height":1776,"width":1080},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/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: {"status":0,"value":{"height":1776,"width":1080}}
> info: [debug] Responding to client with success: {"status":0,"value":{"height":1776,"width":1080},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/window/current/size 200 8.069 ms - 100 {"status":0,"value":{"height":1776,"width":1080},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/touch/perform {"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf","actions":[{"action":"press","options":{"y":444,"x":540}},{"action":"wait","options":{"ms":6000}},{"action":"moveTo","options":{"y":1509,"x":540}},{"action":"release","options":{}}]}
> info: [debug] Pushing command to appium work queue: ["swipe",{"startX":540,"startY":444,"endX":540,"endY":1509,"steps":168}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"swipe","params":{"startX":540,"startY":444,"endX":540,"endY":1509,"steps":168}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: swipe
> info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][1080,1776]
> info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][1080,1776]
> info: [debug] [BOOTSTRAP] [debug] Swiping from [x=540.0, y=444.0] to [x=540.0, y=1509.0] with steps: 168
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":true}
> info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/touch/perform 200 3418.598 ms - 76 {"status":0,"value":true,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/element {"using":"xpath","sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf","value":"//android.widget.TextView[@text='体检报告解读']"}
> info: [debug] Waiting up to 30000ms for condition
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.TextView[@text='体检报告解读']","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.TextView[@text='体检报告解读']","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 //android.widget.TextView[@text='体检报告解读'] using XPATH with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[CLASS=android.widget.TextView, INSTANCE=1]
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":{"ELEMENT":"1"}}
> info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/element 200 230.216 ms - 87 {"status":0,"value":{"ELEMENT":"1"},"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/element/1/click {"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf","id":"1"}
> info: [debug] Pushing command to appium work queue: ["element:click",{"elementId":"1"}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"1"}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: click
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":true}
> info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- POST /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/element/1/click 200 676.049 ms - 76 {"status":0,"value":true,"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: --> GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/contexts {}
> info: [debug] Getting a list of available webviews
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "cat /proc/net/unix"
> info: [debug] Available contexts: 
> info: [debug] []
> info: [debug] Available contexts: NATIVE_APP
> info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_APP"],"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: <-- GET /wd/hub/session/b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf/contexts 200 63.093 ms - 86 {"status":0,"value":["NATIVE_APP"],"sessionId":"b8cdbf8a-11a2-4cfe-9afd-6a2d7913acdf"}
> info: [debug] Didn't get a new command in 60 secs, shutting down...
> info: Shutting down appium session
> info: [debug] Pressing the HOME button
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "input keyevent 3"
> info: [debug] Resetting IME to 'com.baidu.input_huawei/.ImeService'
> info: [debug] executing cmd: E:\tools\App_tools\Android_SDK\platform-tools\adb.exe -s PJQDU16715003110 shell "ime set com.baidu.input_huawei/.ImeService"
> 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] Sent shutdown command, waiting for UiAutomator to stop...
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
> 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] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
> info: [debug] [UIAUTOMATOR STDOUT] Time: 89.276
> 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] We shut down because no new commands came in


↙↙↙阅读原文可查看相关链接,并与作者交流