问答 切换 context 报错,有么有解决方法。

彭帅 · 2018年09月10日 · 最后由 mackle 回复于 2020年11月20日 · 2714 次阅读
contexts = driver.contexts
print(contexts[0])
print(contexts[1])
driver.switch_to.context(contexts[1])
now = driver.current_context
print(now)

报错信息:

Traceback (most recent call last):
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 211, in <module>
    singlesession=args.single_session)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 205, in main
    debug_main(addr, name, kind, *extra, **kwargs)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 30, in debug_main
    run_file(address, name, *extra, **kwargs)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 64, in run_file
    run(argv, addr, **kwargs)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 125, in _run
    _pydevd.main()
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1099, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1106, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\Users\Penn\.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "c:\Program Files\ETC\ETC_UI\ETC.py", line 53, in <module>
    driver.switch_to.context(contexts[1])
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\appium\webdriver\switch_to.py", line 31, in context
    self._driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {'name': context_name})
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 534, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 1346, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 1321, in do_open
    r = h.getresponse()
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Penn\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
Press any key to continue . . .

Appium:

[HTTP] --> POST /wd/hub/session/8c961ae2-8333-4e71-bc0c-795dda358356/context
[HTTP] {"name":"WEBVIEW_unknown","sessionId":"8c961ae2-8333-4e71-bc0c-795dda358356"}
[debug] [W3C] Calling AppiumDriver.setContext() with args: ["WEBVIEW_unknown","8c961ae2-8333-4e71-bc0c-795dda358356"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running 'C:\Program Files\android-sdk\sdk\platform-tools\adb.exe -P 5037 -s TCJRYLG6INQSKRP7 shell cat /proc/net/unix'
[debug] [AndroidDriver] WEBVIEW_9697 mapped to pid 9697
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running 'C:\Program Files\android-sdk\sdk\platform-tools\adb.exe -P 5037 -s TCJRYLG6INQSKRP7 shell ps'
[debug] [AndroidDriver] WEBVIEW_9714 mapped to pid 9714
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running 'C:\Program Files\android-sdk\sdk\platform-tools\adb.exe -P 5037 -s TCJRYLG6INQSKRP7 shell ps'
[debug] [AndroidDriver] Returning process name: 'unknown'
[debug] [AndroidDriver] Returning process name: 'unknown'
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_unknown","WEBVIEW_unknown"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_unknown"]
[debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_unknown'
[debug] [AndroidDriver] A port was not given, using random port: 8000
[debug] [Chromedriver] Changed state to 'starting'
[debug] [Chromedriver] The following Chromedriver executables were found:
[debug] [Chromedriver]     C:/Users/Penn/AppData/Roaming/npm/node_modules/appium/node_modules/appium-chromedriver/chromedriver/win/chromedriver.exe (minimum Chrome version '57.0.2987')
[debug] [ADB] Getting package info for 'com.google.android.webview'
[debug] [ADB] Running 'C:\Program Files\android-sdk\sdk\platform-tools\adb.exe -P 5037 -s TCJRYLG6INQSKRP7 shell dumpsys package com.google.android.webview'
[debug] [Chromedriver] Found Chrome bundle 'com.google.android.webview' version '62.0.3202'
[debug] [Chromedriver] Found 1 Chromedriver executable capable of automating Chrome '62.0.3202'.
[debug] [Chromedriver] Choosing the most recent, 'C:/Users/Penn/AppData/Roaming/npm/node_modules/appium/node_modules/appium-chromedriver/chromedriver/win/chromedriver.exe'.
[debug] [Chromedriver] If a specific version is required, specify it with the `chromedriverExecutable`desired capability.
[Chromedriver] Set chromedriver binary as: C:/Users/Penn/AppData/Roaming/npm/node_modules/appium/node_modules/appium-chromedriver/chromedriver/win/chromedriver.exe
[debug] [Chromedriver] Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"8000 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT
%b=="" TASKKILL /F /PID %a))
[Chromedriver] No old chromedrivers seemed to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running 'C:\Program Files\android-sdk\sdk\platform-tools\adb.exe -P 5037 -s TCJRYLG6INQSKRP7 forward --list'
[Chromedriver] Spawning chromedriver with: C:/Users/Penn/AppData/Roaming/npm/node_modules/appium/node_modules/appium-chromedriver/chromedriver/win/chromedriver.exe --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.29.461591'
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"10.0.17134\"}}}"
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"TCJRYLG6INQSKRP7"}}}
[debug] [JSONWP Proxy] Got response with status 200: {"sessionId":"e4fe0100c0e99ef53c9a1beecb61e15f","status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.17134 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
[Chromedriver]   (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.17134 x86_64))
[Chromedriver]     at Object.wrappedLogger.errorAndThrow (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\appium-support\lib\logging.js:78:13)
[Chromedriver]     at Chromedriver.callee$2$0$ (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:411:13)
[Chromedriver]     at tryCatch (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
[Chromedriver]     at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
[Chromedriver]     at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
[Chromedriver]     at GeneratorFunctionPrototype.invoke (C:\Users\Penn\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[Chromedriver]     at <anonymous>
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"TCJRYLG6INQSKRP7"}}}
[debug] [AndroidBootstrap] Emitting alert message...
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Emitting system alert message
[debug] [JSONWP Proxy] Got response with status 200: {"sessionId":"83c0b5de47a260402971260f99aea264","status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.17134 x86_64)"}}
共收到 19 条回复 时间 点赞

chrome driver 版本不对

雨夜狂奔 回复

版本已经替换了

彭帅 回复

报错明明是版本不对,你把 appiun 日志贴出来

雨夜狂奔 回复

贴的内容被我修改了一遍,你再看看。

彭帅 回复

你这个错跟你最开始发帖都不一样了。。 这个错很多人遇到(chrome not reachable),我也提了 issue,不知道最新发布的 1.9.0 解决没(我没试过),你可以多找几个合适的 chromedriver 再试试,或者只有看看有其他人解决没

雨夜狂奔 回复

看到有人提了在配置中加上:

chromeOptions: {
      androidProcess:"com.tencent.mm:tools"
    }

但我加了,又变成另外一个问题。😁

彭帅 回复

他们这是微信小程序的,普通 app 的 webview 不用加,感觉是 appium 的 bug。。

雨夜狂奔 回复

内吐血,我的就是微信小程序。
改成这个后,就告诉我:
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: A new session could not be created. (Original error: session not created exception: Chrome version must be >= 62.0.3202.0

彭帅 回复

你这个就是 chrome driver 版本不对

彭帅 #10 · 2018年09月11日 Author
雨夜狂奔 回复


提示的是这个鬼版本太低了,但是不知道怎么升级。

彭帅 回复
  1. 你只能降低你的 chromedriver 版本
  2. 如果你开启了 appium 的 chromedriver 版本自动发现,它首先获取你手机 webview 的版本,但是微信并不是使用的你手机的 webview,所以版本也不对的
彭帅 #12 · 2018年09月11日 Author
雨夜狂奔 回复

也是。
但现在换成对应微信 webview 版本了,就开始提示之前遇到的问题
Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable

emm。我好像遇到过,但是不是这个报错。是提示 unable to discover open pages,最后怎么解决的。是因为我们 app 里面 webview 全部改成了腾讯那个 x5 的。所以要需要在 app 内想办法打开调试地址 http://debugx5.qq.cominspect 开关,这样才算是打开 x5 的 webview。试试?还要一个是在 desired_caps 里面加一个,然后点击信息里面的打开 desired_caps['recreateChromeDriverSessions'] = 'true',这个是解决多次切换 context 的时候,chromedriver 重启的方法

彭帅 #14 · 2018年09月11日 Author
cece0417 回复

嗯...
你这个腾讯内核我肯定是开了的。

安卓什么版本?

彭帅 #16 · 2018年09月12日 Author
Jacc 回复
Android System WebView
62.0.3202.84
彭帅 回复

不是 webview, 系统版本 6.0 7.0 8.0?8.0 应该是有问题的,微信里的版本应该是 57 吧

1,版本不对
2,有的时候版本对了,也是报错(之前用的是虚拟机调试的,更换了虚拟机结果成功了)

楼主解决了嘛,可真头疼

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