Appium Appium 切换多个 webview 如果切换到一个没有数据的页面, 再使用 driver.page_source 程序就一直卡着

Dorom · 2019年12月29日 · 最后由 Dorom 回复于 2019年12月30日 · 2908 次阅读

场景

我现在有很多个 handle, 我也不知道要进入哪个 handle,只有进入后获取到 handle 的title 或者 page的内容 判断 才能确定我要切换的 handle

问题

首先有多个 handles :[ 'CDwindow-74F23AC6F2E02CD49C80D105A59D9B88', 'CDwindow-E610CCBB026C5F2FA6C545A72D08344C', 'CDwindow-6FB6B9E476812C956796A6905BAFBE3A', 'CDwindow-B3DD449A8911B62F5C4F600EB4D345E2', 'CDwindow-A2D6E53D6E064CC9D5926F6E4971E9A1', 'CDwindow-0496AD7AB3EFB0BF1F7E27B6E57725A7']

如果切换的 handle 页面有数据的话代码是正常的 , 如果第二个 handle 页面没有数据: 执行 driver.switch_to.window(CDwindow-E610CCBB026C5F2FA6C545A72D08344C) 是正常的,切换成功了,再获取page的时候,程序就卡住了, 如果是空页面,就拿不到page 或title

比如这样的就拿不到

  • Appium 错误日志

[35m[W3C (093221ab)][39m Driver proxy active, passing request on via HTTP proxy
[debug] [35m[WD Proxy][39m Matched '/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title' to command name 'title'
[debug] [35m[WD Proxy][39m Proxying [GET /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title] to [GET http://127.0.0.1:8012/wd/hub/session/0d3068d119621ed957bbf365dade0a57/title] with body: {}
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"0d3068d119621ed957bbf365dade0a57","status":0,"value":"BT学院"}
[35m[WD Proxy][39m Replacing sessionId 0d3068d119621ed957bbf365dade0a57 with 093221ab-b7c1-4c2d-9a8d-a98ac543fe18
[35m[HTTP][39m [37m<-- GET /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title [39m[32m200[39m [90m117 ms - 71[39m
[35m[HTTP][39m [90m[39m
[35m[HTTP][39m [37m-->[39m [37mPOST[39m [37m/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/window[39m
[35m[HTTP][39m [90m{"handle":"CDwindow-F2080AA93040E968384C002645C2267A","sessionId":"093221ab-b7c1-4c2d-9a8d-a98ac543fe18"}[39m
[35m[W3C (093221ab)][39m Driver proxy active, passing request on via HTTP proxy
[debug] [35m[WD Proxy][39m Matched '/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/window' to command name 'setWindow'
[debug] [35m[Protocol Converter][39m Copied 'handle' value 'CDwindow-F2080AA93040E968384C002645C2267A' to 'name' as per JSONWP spec
[debug] [35m[WD Proxy][39m Proxying [POST /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/window] to [POST http://127.0.0.1:8012/wd/hub/session/0d3068d119621ed957bbf365dade0a57/window] with body: {"handle":"CDwindow-F2080AA93040E968384C002645C2267A","sessionId":"093221ab-b7c1-4c2d-9a8d-a98ac543fe18","name":"CDwindow-F2080AA93040E968384C002645C2267A"}
[debug] [35m[WD Proxy][39m Got response with status 200: {"sessionId":"0d3068d119621ed957bbf365dade0a57","status":0,"value":null}
[35m[WD Proxy][39m Replacing sessionId 0d3068d119621ed957bbf365dade0a57 with 093221ab-b7c1-4c2d-9a8d-a98ac543fe18
[35m[HTTP][39m [37m<-- POST /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/window [39m[32m200[39m [90m15 ms - 65[39m
[35m[HTTP][39m [90m[39m
[35m[HTTP][39m [37m-->[39m [37mGET[39m [37m/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title[39m
[35m[HTTP][39m [90m{}[39m
[35m[W3C (093221ab)][39m Driver proxy active, passing request on via HTTP proxy
[debug] [35m[WD Proxy][39m Matched '/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title' to command name 'title'
[debug] [35m[WD Proxy][39m Proxying [GET /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title] to [GET http://127.0.0.1:8012/wd/hub/session/0d3068d119621ed957bbf365dade0a57/title] with body: {}
[35m[WD Proxy][39m Got an unexpected response with status undefined: {"code":"ESOCKETTIMEDOUT","connect":false}
[debug] [35m[W3C (093221ab)][39m Encountered internal error running command: Error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: Error: ESOCKETTIMEDOUT
[debug] [35m[W3C (093221ab)][39m at doJwpProxy (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:490:13)
[35m[HTTP][39m [37m<-- GET /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/title [39m[31m500[39m [90m240051 ms - 717[39m
[35m[HTTP][39m [90m[39m
[35m[HTTP][39m [37m-->[39m [37mPOST[39m [37m/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/element[39m
[35m[HTTP][39m [90m{"using":"id","value":"cn.btclass.sierra:id/tv_right_notice","sessionId":"093221ab-b7c1-4c2d-9a8d-a98ac543fe18"}[39m
[35m[W3C (093221ab)][39m Driver proxy active, passing request on via HTTP proxy
[debug] [35m[WD Proxy][39m Matched '/wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/element' to command name 'findElement'
[debug] [35m[WD Proxy][39m Proxying [POST /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/element] to [POST http://127.0.0.1:8012/wd/hub/session/0d3068d119621ed957bbf365dade0a57/element] with body: {"using":"id","value":"cn.btclass.sierra:id/tv_right_notice","sessionId":"093221ab-b7c1-4c2d-9a8d-a98ac543fe18"}
[35m[WD Proxy][39m Got an unexpected response with status undefined: {"code":"ESOCKETTIMEDOUT","connect":false}
[debug] [35m[W3C (093221ab)][39m Encountered internal error running command: Error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: Error: ESOCKETTIMEDOUT
[debug] [35m[W3C (093221ab)][39m at doJwpProxy (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:490:13)
[35m[HTTP][39m [37m<-- POST /wd/hub/session/093221ab-b7c1-4c2d-9a8d-a98ac543fe18/element [39m[31m500[39m [90m240010 ms - 717[39m
[35m[HTTP][39m [90m[39m

最佳回复

找了一个临时的解决方法:
1、 获取当前的sessionId
2、 拼接 url
3、 请求 设置超时时间请求 url
4、 如果超时说明 handle 是无效的

灵感来源

session_id = self.driver.session_id
 url = "http://127.0.0.1:4723/wd/hub/session/{0}/title".format(session_id)
try:
  res = requests.get(url,timeout=3)
except:
  dosomething
共收到 4 条回复 时间 点赞
Dorom 回复

是不是从正向解决比较好,如果没有内容的 page 可以不要的话,那就提前判断啊

恒温 回复

问题就在于我也不知道 page 有没有内容, 最初是自己计算需要切换 handle,但有时候 handle list 加载就不规律,达不到预期。

找了一个临时的解决方法:
1、 获取当前的sessionId
2、 拼接 url
3、 请求 设置超时时间请求 url
4、 如果超时说明 handle 是无效的

灵感来源

session_id = self.driver.session_id
 url = "http://127.0.0.1:4723/wd/hub/session/{0}/title".format(session_id)
try:
  res = requests.get(url,timeout=3)
except:
  dosomething
Dorom 关闭了讨论 12月30日 20:29
Dorom 重新开启了讨论 12月30日 20:30
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册