Appium python 切换 webview 报错找不到元素

April · 2016年04月14日 · 最后由 81—1 回复于 2016年04月14日 · 2071 次阅读

1、代码

def find_html_element(self, loc):
        try:
            time.sleep(5)
            webview = self.driver.contexts[1]
            self.driver.switch_to.context(webview)
            WebDriverWait(self.driver,10).until(lambda driver : self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except:
            self.method.save_error_pic(self.driver, "Find_Element_Error")
            raise ValueError("定位元素错误:\n1、当前测试用例为:%s\n2、页面中未能定位到元素,定位方式为:%s 定位值为:%s" % (self, loc[0],loc[1]))

2、appium log

Launching Appium server with command: E:\appium\node.exe lib\server\main.js --address 127.0.0.1 --port 4723 --platform-name Android --platform-version 17 --automation-name Appium --log-no-color
info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)
info: Appium REST http interface listener started on 127.0.0.1:4723
info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"platformName":"Android","platformVersion":"17","automationName":"Appium"}
info: Console LogLevel: debug
info: --> POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"5.0\r\n","deviceName":"0b64bc5c","platformName":"Android","appActivity":"com.snowstore.app.prometheus.StartMovieActivity","appPackage":"com.snowstore.app.prometheus"}}
info: Client User-Agent string: Python-urllib/2.7
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 1b32ca4d-6c25-4449-b49d-cf983fc4b5f5
info: Starting android appium
info: [debug] Getting Java version
info: Java version is: 1.7.0_79
info: [debug] Checking whether adb is present
info: [debug] Using adb from E:\android\android-sdk-windows\platform-tools\adb.exe
warn: No app capability, can't parse package/activity
info: [debug] Using fast reset? true
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: E:\android\android-sdk-windows\platform-tools\adb.exe devices
info: [debug] 1 device(s) connected
info: Found device 0b64bc5c
info: [debug] Setting device id to 0b64bc5c
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c wait-for-device
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "echo 'ready'"
info: [debug] Starting logcat capture
info: [debug] Getting device API level
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 21
info: Device API level is: 21
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: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c 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: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c forward tcp:4724 tcp:4724
info: [debug] Pushing appium bootstrap to device...
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c push "E:\appium\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.snowstore.app.prometheus -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] 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:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 21
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.snowstore.app.prometheus/com.snowstore.app.prometheus.StartMovieActivity"
info: [debug] Waiting for pkg "com.snowstore.app.prometheus" and activity "com.snowstore.app.prometheus.StartMovieActivity" to be focused
info: [debug] Getting focused package and activity
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "dumpsys window windows"
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "getprop ro.build.version.release"
info: [debug] Device is at release version 5.0
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 1b32ca4d-6c25-4449-b49d-cf983fc4b5f5
info: <-- POST /wd/hub/session 303 4611.951 ms - 74
info: --> GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5 {}
info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.0","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformVersion":"5.0\r\n","deviceName":"0b64bc5c","platformName":"Android","appActivity":"com.snowstore.app.prometheus.StartMovieActivity","appPackage":"com.snowstore.app.prometheus"},"deviceName":"0b64bc5c","platformName":"Android","appActivity":"com.snowstore.app.prometheus.StartMovieActivity","appPackage":"com.snowstore.app.prometheus"},"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: <-- GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5 200 2.638 ms - 669 {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"5.0","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformVersion":"5.0\r\n","deviceName":"0b64bc5c","platformName":"Android","appActivity":"com.snowstore.app.prometheus.StartMovieActivity","appPackage":"com.snowstore.app.prometheus"},"deviceName":"0b64bc5c","platformName":"Android","appActivity":"com.snowstore.app.prometheus.StartMovieActivity","appPackage":"com.snowstore.app.prometheus"},"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: --> GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/contexts {}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "cat /proc/net/unix"
info: [debug] WEBVIEW_10665 mapped to pid 10665
info: [debug] Getting process name for webview
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "ps"
info: [debug] Parsed pid: 10665 pkg: com.snowstore.app.prometheus
info: [debug] from: u0_a242,10665,318,1854232,180564,ffffffff,00000000,S,com.snowstore.app.prometheus
info: [debug] returning process name: com.snowstore.app.prometheus
info: [debug] Available contexts:
info: [debug] ["WEBVIEW_com.snowstore.app.prometheus"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.snowstore.app.prometheus
info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_APP","WEBVIEW_com.snowstore.app.prometheus"],"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: <-- GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/contexts 200 565.989 ms - 125 {"status":0,"value":["NATIVE_APP","WEBVIEW_com.snowstore.app.prometheus"],"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: --> POST /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/context {"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5","name":"WEBVIEW_com.snowstore.app.prometheus"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "cat /proc/net/unix"
info: [debug] WEBVIEW_10665 mapped to pid 10665
info: [debug] Getting process name for webview
info: [debug] executing cmd: E:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "ps"
info: [debug] Parsed pid: 10665 pkg: com.snowstore.app.prometheus
info: [debug] from: u0_a242,10665,318,1854232,181076,ffffffff,00000000,S,com.snowstore.app.prometheus
info: [debug] returning process name: com.snowstore.app.prometheus
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.snowstore.app.prometheus
info: [debug] ["WEBVIEW_com.snowstore.app.prometheus"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.snowstore.app.prometheus
info: [debug] Connecting to chrome-backed webview
info: Chromedriver: Changed state to 'starting'
info: Chromedriver: Set chromedriver binary as: E:\appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
info: Chromedriver: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (netstat -nao ^| findstr /R /C:"9515 ") do (FOR /F "usebackq" %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe) do (IF NOT %b=="" TASKKILL /F /PID %a))
info: Chromedriver: No old chromedrivers seemed to exist
info: Chromedriver: Spawning chromedriver with: E:\appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
info: Chromedriver: [STDOUT] Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 9515
Only local connections are allowed.
info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"6.1 SP1\"}}}"
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.snowstore.app.prometheus","androidUseRunningApp":true,"androidDeviceSerial":"0b64bc5c"}}}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"7be5cbf03d49cffc9a15404f4cf3a326","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{},"cssSel...
info: Chromedriver: Changed state to 'online'
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: <-- POST /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/context 200 4566.894 ms - 76 {"status":0,"value":null,"sessionId":"1b32ca4d-6c25-4449-b49d-cf983fc4b5f5"}
info: --> GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/screenshot {}
info: JSONWP Proxy: Proxying [GET /wd/hub/session/1b32ca4d-6c25-4449-b49d-cf983fc4b5f5/screenshot] to [GET http://127.0.0.1:9515/wd/hub/session/7be5cbf03d49cffc9a15404f4cf3a326/screenshot] with body: {}
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:\android\android-sdk-windows\platform-tools\adb.exe -s 0b64bc5c shell "input keyevent 3"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Stopping chromedriver for context WEBVIEW_com.snowstore.app.prometheus
info: Chromedriver: Changed state to 'stopping'
info: JSONWP Proxy: Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/7be5cbf03d49cffc9a15404f4cf3a326] with no body
希望大神帮忙看看

共收到 4 条回复 时间 点赞

Chromedriver 版低了,升级后再试试

#1 楼 @rubyvirus 以上是封装的方法。但是直接写脚本是可以的

def test_login(self):
        time.sleep(5)
        webview = self.driver.contexts[1]
        self.driver.switch_to.context(webview)
        WebDriverWait(self.driver,10).until(lambda driver : self.driver.find_element_by_class_name("more_invest").is_displayed())
        self.driver.find_element_by_class_name("more_invest").click()
        time.sleep(10)

#1 楼 @rubyvirus 怎么看到 chomedriver 版本的?

#3 楼 @mymgbaby info: Chromedriver: [STDOUT] Starting ChromeDriver 2.18.343845

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