Appium python+appium 切换 WEBVIEW 时,经常报错

想飞的菜鸟 · 2015年08月31日 · 最后由 H.L 回复于 2023年08月25日 · 3728 次阅读

新手,不知道为什么切换 WEBVIEW 的时候,老是报错。
日志如下:

info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.paic.ibankadnroidsmp
info: [debug] ["WEBVIEW_com.paic.ibankadnroidsmp"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.paic.ibankadnroidsmp
info: [debug] Connecting to chrome-backed webview
info: Set chromedriver binary as: F:\mysoft\Appium1.4\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: 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: No old chromedrivers seemed to exist
info: Spawning chromedriver with: F:\mysoft\Appium1.4\node_modules\appium\build\chromedriver\windows\chromedriver.exe --url-base=wd/hub --port=9515
info: [CHROMEDRIVER STDOUT] Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 9515
Only local connections are allowed.
info: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: Got response with status 200: {"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Windows NT","version":"6.3"}}}
info: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.paic.ibankadnroidsmp","androidUseRunningApp":true,"androidDeviceSerial":"NX511J"}}}
info: Got response with status 200: {"sessionId":"b87040b60891df760f1c109d27a2c150","status":13,"value":{"message":"unknown error: Device NX511J is not online\n (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a203268398...
info: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.paic.ibankadnroidsmp","androidUseRunningApp":true,"androidDeviceSerial":"NX511J"}}}
info: Got response with status 200: {"sessionId":"63960ff2cde290f6daa6f6fe77ee078e","status":13,"value":{"message":"unknown error: Device NX511J is not online\n (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a203268398...
info: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.paic.ibankadnroidsmp","androidUseRunningApp":true,"androidDeviceSerial":"NX511J"}}}
info: Got response with status 200: {"sessionId":"4c9314129cb798504e88618de9cfaf04","status":13,"value":{"message":"unknown error: Device NX511J is not online\n (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a203268398...
info: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.paic.ibankadnroidsmp","androidUseRunningApp":true,"androidDeviceSerial":"NX511J"}}}
info: Got response with status 200: {"sessionId":"aed9c4de7077e75f28fb04e51503512f","status":13,"value":{"message":"unknown error: Device NX511J is not online\n (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a203268398...

部分代码如下:

def test_ibank(self):

    logging.info('程序启动中,请稍等……')
    #time.sleep(20)    
    #logging.FileHandler('e:/auto_test_log.log')
    start = time.clock()
    #等待程序启动
    while True:
        try:
            #查找登陆按钮 ,以找到登陆按钮提示启动成功
            el = self.driver.find_element_by_id("com.paic.ibankadnroidsmp:id/top_right_tx") 
        except Exception:
            continue
        else:
            end = time.clock()
            logging.info('程序启动成功')
            #输出启动耗时
            logging.info('程序启动用时: %f s'%(end-start))
            break

    logging.info('点击登陆按钮')        
    el.click()    
    time.sleep(2)
    context=self.driver.contexts
    self.assertEqual(2, len(context))
    time.sleep(5)
    self.driver.switch_to.context('WEBVIEW')
    time.sleep(1)
    el=self.driver.find_element_by_id('userName')
    #el.click
    el.send_keys('username')
    js_password="$(\"#login_password\").customInput(\'setValue\',\'password\')"
    try:
        self.driver.execute_script(js_password)
    except Exception as e:
        logging.info('JS脚本执行错误,错误:%s'%e)

但是我用 ADB 的时候,真机是可以找到的!!!而且有时候能过,有时候不能过!!有么有 人知道,求高手。真心感谢!!

共收到 17 条回复 时间 点赞

info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.paic.ibankadnroidsmp
webview 后边的 com.什么也要写上
可以打印出所有 contexts 的内容,逐个试试,我这边的情况是,有时候所用的 webview 还会变化...

self.driver.page_source 看看

#1 楼 @henry 都试过了,没用。一样的,时好时坏!!!

时好时坏,可能与页面加载时间有关系,把等待时间设置长一点儿,试试吧

相同的问题,切换下 webview 好了, 然后继续不能用

同样的问题,有时能用,有时不能用

现在 appium 支持 H5 了~定位 webview 内的元素妥妥的~至少我们的应用没问题~

#7 楼 @jennyhui 你现在用的 appium 是什么版本???

我目前也遇到类似问题,报错如下
Server 端:

info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: read ECONNRESET)","code":"ECONNRESET","errno":"ECONNRESET","syscall":"read","origValue":"read ECONNRESET"},"sessionId":null}

info: <-- DELETE /wd/hub/session/70bb307a-dc0d-ca4c-54e1-8cdfe25a0a60 500 18.661 ms - 242

info: [debug] [SELENDROID ERR] No address associated with hostname

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.m.a(NetUtilsProcessor.java:500)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.m.a(NetUtilsProcessor.java:698)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.c.b.b(SecureOpenUrlWithParams.java:26)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.c.b.a(SecureOpenUrlWithParams.java:21)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.c.a.a(NetSecure.java:71)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.l.a(NetUtils.java:509)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.l.a(NetUtils.java:470)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.l.a(NetUtils.java:455)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.l.a(NetUtils.java:450)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.l.a(NetUtils.java:445)

info: [debug] [SELENDROID ERR] at com.sina.weibo.net.f.a(NetEngine.java:6715)

info: [debug] [SELENDROID ERR] at com.sina.weibo.localpush.c$a.a(LocalPushService.java:304)

info: [debug] [SELENDROID ERR] at com.sina.weibo.localpush.c$a.doInBackground(LocalPushService.java:274)

info: [debug] [SELENDROID ERR] at android.os.AsyncTask$2.call(AsyncTask.java:288)

info: [debug] [SELENDROID ERR] at java.util.concurrent.FutureTask.run(FutureTask.java:237)

info: [debug] [SELENDROID ERR] at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

info: [debug] [SELENDROID ERR] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

info: [debug] [SELENDROID ERR] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

info: [debug] [SELENDROID ERR] at java.lang.Thread.run(Thread.java:841)

info: [debug] [SELENDROID ERR] No address associated with hostname

client 的 log:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (Original error: read ECONNRESET) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 31 milliseconds
Build info: version: '2.46.0', revision: '61506a4624b13675f24581e453592342b7485d71', time: '2015-06-04 10:22:50'
System info: host: 'CN00200670', ip: '150.236.223.180', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Session ID: ecc0a543-7f96-93c8-e28d-5e2f354d7d47
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{automationName=selendroid, platform=ANDROID, app=C:\Jackie\Work\MultiClient\Codes\appnium\apps\com.customer.brand.tvclient-1.0.apk, acceptSslCerts=true, javascriptEnabled=true, browserName=selendroid, networkConnectionEnabled=true, rotatable=true, desired={automationName=Selendroid, app=C:\Jackie\Work\MultiClient\Codes\appnium\apps\com.customer.brand.tvclient-1.0.apk, platformVersion=4.1.2, platformName=Android, deviceName=GT-I9300}, version=0.15.0, platformVersion=19, deviceName=GT-I9300, platformName=android, handlesAlerts=true, warnings={}, takesScreenshot=true}]

我试了一下 driver.context("NATIVE_APP");是可以执行的
driver.context("WEB_VIEW0");就执行不了
各位有遇到这个问题的吗

#1 楼 @henry
driver.context("WEBVIEW_com.。。。");执行不了。
driver.context("WEB_VIEW");可以执行。
但是 Activity 上有复数个 webview。打印出 contexts 的内容,只有一个 web view,??
是移动端混合型 App(hybrid app),各位有遇到这个问题的吗

info: --> GET /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/contexts {}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: /Users/Desktop/android-sdk-macosx/platform-tools/adb -s R32CA05X1CJ shell "cat /proc/net/unix"
info: [debug] WEBVIEW_9615 mapped to pid 9615
info: [debug] Getting process name for webview
info: [debug] executing cmd: /Users/Desktop/android-sdk-macosx/platform-tools/adb -s R32CA05X1CJ shell "ps"
info: [debug] Parsed pid: 9615 pkg: com.systemdirector.ent.smt
info: [debug] from: u0_a302,9615,136,1669508,128240,ffffffff,00000000,S,com.systemdirector.ent.smt
info: [debug] returning process name: com.systemdirector.ent.smt
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.systemdirector.ent.smt
info: [debug] ["WEBVIEW_com.systemdirector.ent.smt"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.systemdirector.ent.smt
info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_APP","WEBVIEW_com.systemdirector.ent.smt"],"sessionId":"3920d29c-9de2-4908-a663-73a30f423f33"}
info: <-- GET /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/contexts 200 218.819 ms - 130 {"status":0,"value":["NATIVE_APP","WEBVIEW_com.systemdirector.ent.smt"],"sessionId":"3920d29c-9de2-4908-a663-73a30f423f33"}
info: --> POST /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/context {"name":"WEBVIEW_com.systemdirector.ent.smt"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: /Users/Desktop/android-sdk-macosx/platform-tools/adb -s R32CA05X1CJ shell "cat /proc/net/unix"
info: [debug] WEBVIEW_9615 mapped to pid 9615
info: [debug] Getting process name for webview
info: [debug] executing cmd: /Users/Desktop/android-sdk-macosx/platform-tools/adb -s R32CA05X1CJ shell "ps"
info: [debug] Parsed pid: 9615 pkg: com.systemdirector.ent.smt
info: [debug] from: u0_a302,9615,136,1669508,128240,ffffffff,00000000,S,com.systemdirector.ent.smt
info: [debug] returning process name: com.systemdirector.ent.smt
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.systemdirector.ent.smt
info: [debug] ["WEBVIEW_com.systemdirector.ent.smt"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.systemdirector.ent.smt
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"3920d29c-9de2-4908-a663-73a30f423f33"}
info: <-- POST /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/context 200 149.636 ms - 76 {"status":0,"value":null,"sessionId":"3920d29c-9de2-4908-a663-73a30f423f33"}
info: --> GET /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/orientation {}
info: [debug] Proxying command to 127.0.0.1:9515
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/11ed082b6b9a7dd33d791bad558c5bce/orientation,method:GET"}
info: [debug] Proxied response received with status 501: "unimplemented command: session/11ed082b6b9a7dd33d791bad558c5bce/orientation"
info: <-- GET /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33/orientation 501 7.323 ms - 75
info: --> DELETE /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33 {}
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: /Users/Desktop/android-sdk-macosx/platform-tools/adb -s R32CA05X1CJ shell "input keyevent 3"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Deleting Chrome session
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session/11ed082b6b9a7dd33d791bad558c5bce,method:DELETE"}
info: [debug] Cleaning up Chromedriver
info: [debug] Killing chromedriver and waiting for close
info: [debug] Chromedriver exited with code null
info: debug
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: {"status":0,"value":"OK, shutting down"}
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: 42.512
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
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":"3920d29c-9de2-4908-a663-73a30f423f33"}
info: <-- DELETE /wd/hub/session/3920d29c-9de2-4908-a663-73a30f423f33 200 764.713 ms - 76 {"status":0,"value":null,"sessionId":"3920d29c-9de2-4908-a663-73a30f423f33"}

环境:MAC 机,apache-maven-3.3.3,appium-1.4.8.dmg

uiautomator 获取页面元素中文乱码,机型安卓 5.0.2,请问能解决吗

#12 楼 @alisawu 升级安卓系统

14楼 已删除

换个手机试试

@stylenshain 我是切换 web view 的时候 一直处于缓冲状态 不保存也不执行 不知道什么原因
大概过了 1 分钟后 好像切换成功了 但是 click xpath 利用谷歌工具定位 直接报错.....

测试 回复

我也遇到和你相同问题,卡在切换在 webview 页面,在执行语句 self.driver.switch_to.context("WEBVIEW"),既不报错,也不执行下面语句

现在问还能有人回嘛😂 我用 appnium 抓取微信公众号的链接,有时候返回的是正确的 url,有时候就都返回https://mp.weixin.qq.com/__tmpl__/0_6824315-zh_CN-zip.html这个链接,这是为什么呀,有大佬知道嘛

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