Appium [已解决] 运行 hybrid 应用,切换到 webview 格式后报错

· 2015年06月02日 · 最后由 http 回复于 2017年01月13日 · 1637 次阅读

运行 hybrid 应用,切换到 webview 格式后报错

最近一直在搞 hybrid 啊。

  1. 从恒温跟恒捷帮我看第一匹 apk,那是 crosswalk 做的前端页面,貌似 windows 环境下,不支持。起初不知道,蒙头搞了 N 久,以为一定可以获取 webview
  2. 然后这次领导不依不饶,又给我 hybrid 前端页面是 webview 的 apk 了,这次可以获取到 webview 了,但是想切过去又报错,尼玛,怎么玩

我的代码:

#coding=utf-8
from appium import webdriver
from time import sleep

desired_caps = {}
#desired_caps['automationName'] = 'Selendroid'
desired_caps['automationName'] ="android"
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.jiudao.ccare'
desired_caps['appActivity'] = '.MainActivity'
desired_caps['autoWebview'] = 'true'
#desired_caps['androidDeviceSocket'] = 'chrome_devtools_remote'

dr = webdriver.Remote('http://192.168.10.65:4723/wd/hub', desired_caps)

dr.implicitly_wait(10)
a=dr.contexts
print (a)
#time.sleep(3)
dr.switch_to.context("WEBVIEW_com.jiudao.ccare")
username=dr.find_element_by_id("username")
username.clear()
username.send_keys("admin@WOT")
password=dr.find_element_by_id("username")
password.clear()
password.send_keys("1")
dr.close_app()
dr.quit()

这次我是获取到 webview 了,是 WEBVIEW_com.jiudao.ccare

然后还是报错,好蛋疼,请看以下报错信息

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] Getting device API level
info: [debug] executing cmd: D:\BaiduYunDownload\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "getprop ro.build.version.sdk"
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
info: [debug] Device is at API Level 19
info: [debug] executing cmd: D:\BaiduYunDownload\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.jiudao.ccare/.MainActivity"
info: [debug] Waiting for pkg "com.jiudao.ccare" and activity ".MainActivity" to be focused
info: [debug] Getting focused package and activity
info: [debug] executing cmd: D:\BaiduYunDownload\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "dumpsys window windows"
info: [debug] Setting auto webview
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\BaiduYunDownload\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "cat /proc/net/unix"
info: [debug] WEBVIEW_2473 mapped to pid 2473
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\BaiduYunDownload\android-sdk\platform-tools\adb.exe -s 192.168.56.101:5555 shell "ps"
info: [debug] Parsed pid: 2473 pkg: com.jiudao.ccare
info: [debug] from: u0_a64,2473,224,634096,51596,ffffffff,b7729586,R,com.jiudao.ccare
info: [debug] returning process name: com.jiudao.ccare
info: [debug] Available contexts: 
info: [debug] ["WEBVIEW_com.jiudao.ccare"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.jiudao.ccare
info: [debug] Connecting to chrome-backed webview
info: [debug] Creating Chrome session
info: [debug] Set chromedriver binary as: E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [debug] Ensuring Chromedriver exists
info: [debug] 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 %b))
info: [debug] No old chromedrivers seemed to exist
info: [debug] Spawning chromedriver with: E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [debug] [CHROMEDRIVER] Starting ChromeDriver (v2.10.267521) on port 9515
Only local connections are allowed.
info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.jiudao.ccare","androidUseRunningApp":true,"androidDeviceSerial":"192.168.56.101:5555"}}}}
info: [debug] [CHROMEDRIVER STDERR] [0.015][SEVERE]: Could not bind socket to 127.0.0.1:9515
info: [debug] [CHROMEDRIVER] Port not available. Exiting...
info: [debug] Chromedriver exited with code 1
info: [debug] Chromedriver exited unexpectedly
error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"9cf43a0e3328f67792eaf54e61744351","status":13,"value":{"message":"unknown error: Chrome version must be >= 33.0.1750.0\n  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)"}}
error: Unhandled error: TypeError: Cannot read property 'status' of undefined
    at null.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\devices\android\android-hybrid.js:254:21)
    at null.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\devices\android\android-context-controller.js:68:23)
    at null.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\devices\android\android-hybrid.js:165:23)
    at null.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\devices\android\chromedriver.js:222:9)
    at Request._callback (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\devices\common.js:121:5)
    at Request.self.callback (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\node_modules\request\request.js:121:22)
    at Request.EventEmitter.emit (events.js:98:17)
    at Request.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\node_modules\request\request.js:985:14)
    at Request.EventEmitter.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (E:\360Downloads\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\node_modules\request\request.js:936:12) context: [POST /wd/hub/session {"desiredCapabilities":{"appPackage":"com.jiudao.ccare","deviceName":"192.168.56.101:5555","automationName":"android","appActivity":".MainActivity","platformName":"Android","platformVersion":"4.4","au]
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 40 条回复 时间 点赞

log 不是提示 chrome driver 的端口不可用么?

#2 · 2015年06月02日 Author

#1 楼 @cy_suncheng info: [debug] No old chromedrivers seemed to exist?这句话?请问你 qq 多少,qq 能不能细聊

#2 楼 @mads 这里是关键:

> info: [debug] [CHROMEDRIVER STDERR] [0.015][SEVERE]: Could not bind socket to 127.0.0.1:9515
> info: [debug] [CHROMEDRIVER] Port not available. Exiting...

自己检查一下电脑的 9515 端口是不是被占用了?

#2 楼 @mads 或者用 --chromedriver-port 参数改下 chromedriver 的端口。
参考:https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/server-args.cn.md

#3 楼 @chenhengjie123 最直接是不是可以把占用 9515 端口的应用关闭点?然后再运行脚本

#5 楼 @yuwuhen333 也可以。但你要确定那个应用是不是可以关。

#2 楼 @mads
我这边去掉下面的代码
a=dr.contexts
print (a)
#time.sleep(3)
dr.switch_to.context("WEBVIEW_com.jiudao.ccare")
在小米 MI 3W 手机上 Android 版本是 4.4.4 运行是可以输入 username 成功的,现在搞不清楚什么原因

#8 · 2015年06月02日 Author

#7 楼 @yuwuhen333 看到你代码了

#9 · 2015年06月03日 Author

#6 楼 @chenhengjie123

同样的脚本我又运行了,今天刚刚重装好电脑。。。
同样的脚本,模拟器是 4.4 三星 S4,然后运行了出现下图

还有我想问下设置 --chromedriver-port 的参数,启动的时候 appium --chromedriver-port <端口号>这样么?

#9 楼 @mads Yes。文档里不是有例子了吗?

#9 楼 @mads 你是停在截图那个位置不动了?
你设了 autoWebview,不用再用 dr.switch_to.context("WEBVIEW_com.jiudao.ccare") 转 context 了。文档里已经说了设置了 autoWebview 后会自动转 context 到 webview 的。

#12 · 2015年06月03日 Author

#11 楼 @chenhengjie123 是的,我把切换的注销掉还是老样子。。。停着不动

#coding=utf-8
import os
import sys
#from PIL import Image
import unittest
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

desired_caps = {}
#desired_caps['automationName'] = 'Selendroid'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.jiudao.ccare'
desired_caps['appActivity'] = '.MainActivity'
desired_caps['autoWebview'] = 'true'
desired_caps["unicodeKeyboard"] = "True"
desired_caps["resetKeyboard"] = "True"
desired_caps['autoWebview'] = 'true'
dr = webdriver.Remote('http://192.168.10.65:4723/wd/hub', desired_caps)

dr.implicitly_wait(10)
#a=dr.contexts
#print (a)
#time.sleep(3)
#dr.switch_to.context("WEBVIEW_com.jiudao.ccare")
username=dr.find_element_by_id("username")
username.clear()
username.send_keys("admin@WOT")
password=dr.find_element_by_id("username")
password.clear()
password.send_keys("1")
dr.close_app()
dr.quit()

#12 楼 @mads 看了下源码,它调用了 appium-chromedriver 来启动 chromedriver ,下一个 log 应该是在 appium-chromedriver 输出的 Set chromedriver binary as: ...
你卡在这里不知道是否和没能调用到 appium-chromedriver 有关。

你用 appiumForWindows 试过吗?

哈哈哈!这个问题我早就解决了,换一个 ChromeDriver.exe,换成 2.14 版本的就解决了。这个我 google 过,是低版本 ChromeDriver 的 bug。
快谢谢我吧- 3- @mads

#15 · 2015年06月03日 Author

#14 楼 @weamylady 能给个下载地址么
你真的是这个问题,一模一样的?

#15 楼 @mads 你先试试嘛~~ 上网找 Chromedriver.exe,有很多下载的。我下班打球去咯~嘿嘿

#17 · 2015年06月03日 Author

#16 楼 @weamylady 你好潇洒啊,打球。。。这小日子过的

#18 · 2015年06月03日 Author

#17 楼 @mads 你是命令行端还是哪个

#19 · 2015年06月03日 Author

#16 楼 @weamylady 你是命令行端还是哪个

#19 楼 @mads 下载一个替换掉 build 文件夹下面的 ChromeDriver/Windows/下的 ChromeDriver.exe 就行了

#21 · 2015年06月04日 Author

#20 楼 @weamylady 只有 mac 的文件夹,是不是手动自己添加以下

#21 楼 @mads 你是 iOS 的? 那就搞个 Mac 对应的 ChromeDriver 呗,我说的是在 Windows 的情况下

#23 · 2015年06月04日 Author

#22 楼 @weamylady 我是 windows 的命令行。。。发现只有 mac 的文件夹

#23 楼 @mads 肯定有 Windows 文件夹的,你找一找实际的执行目录吧。要不然你的 2.10 版本的 ChromeDriver 哪来的捏?!

#23 楼 @mads C:\Users\xxxxxx\AppData\Roaming\npm\node_modules\appium\build\chromedriver\windows 如果时 cmd 安装的,路径是这个

#26 · 2015年06月04日 Author

#25 楼 @cy_suncheng
我的电脑上没有 windows 的文件夹的,只有 mac,win7 系统

#26 楼 @mads 奇怪,你只装过一次 appium?或者 你全局搜索一下 chromedriver 试试看

#28 · 2015年06月04日 Author

#27 楼 @cy_suncheng 十分灵异,搜了还是不行~

#28 楼 @mads 要不要拷给你,你试试?

#30 · 2015年06月04日 Author

#29 楼 @yuwuhen333 是 GUI?还是命令行

#31 · 2015年06月04日 Author

#29 楼 @yuwuhen333 已用 GUI 端解决,命令行我放弃。以后再说

我也遇到了,建议楼主可以先用电脑 chrome 调试,有时候状态会很明显。

如图。如果有问题这里一下就可以看出来了。
另外,程序找到的 WEBVIEW 是带包名的,可以省略。
我的代码如下:
self.driver.switch_to.context('WEBVIEW')
time.sleep(1)
el=self.driver.find_element_by_id('userName')
#el.click
el.send_keys('username')
el=self.driver.find_element_by_id('login_password')
el.click()

有两个 APK,需要通过 A 应用,才能打开 B 应用。
A 应用为原生,而 B 应用是混合的话,进入 B 应用切换 WEBVIEW 时,会提示 chrome not reachable。
需要怎么操作?

先收藏

@mads 你好,请问你说的 "貌似 windows 环境下,不支持" 是什么意思? 是 UI Automator 无法获取到对象?

#36 · 2016年03月15日 Author

#35 楼 @finelucky 不支持 crosswalk 做的 webview

#2 楼 @mads 请问,当我的 apk 切换到 webview 后,定位到 h5 元素到 app 中运行,为什么不执行么,也不报错,android4.4.4

#38 · 2016年07月15日 Author

#37 楼 @nsm 你确定切到了么,还要你的定位元素怎么写的。。

请问楼主最后如何解决的,我遇到的是切换 context 之后卡住不动了

@tianyinjiao 海角 你我也是 chrome not reachable 请问你搞定了吗

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