Appium 1.5.3 使用遇到的两个问题

adfghzhang · 2016年08月08日 · 最后由 测试老菜 回复于 2016年11月01日 · 1568 次阅读

环境:

  • CentOS 7.2
  • Appium 1.5.3

问题 1:

使用 driver.findElementByAccessibilityId("退出登录") 查找不到该元素,实际页面已经切到对应页面 且该按钮已经出现。请使用过 1.5.x 的同学分享一下经验,谢谢!

Appium Log:

2016-08-08 14:17:40:053 - info: [HTTP] --> POST /wd/hub/session/e7202c53-50b2-4981-9628-9ceaaccb9606/element {"using":"accessibility id","value":"退出登录"}
2016-08-08 14:17:40:053 - info: [MJSONWP] Calling AppiumDriver.findElement() with args: ["accessibility id","退出登录",...
2016-08-08 14:17:40:054 - info: [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
2016-08-08 14:17:40:054 - info: [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
2016-08-08 14:17:40:054 - info: [debug] [BaseDriver] Waiting up to 0 ms for condition
2016-08-08 14:17:40:055 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"accessibility id","selector":"退出登录","context":"","multiple":false}}
2016-08-08 14:17:40:062 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"accessibility id","selector":"退出登录","context":"","multiple":false}}
2016-08-08 14:17:40:066 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
2016-08-08 14:17:40:067 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
2016-08-08 14:17:40:067 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '退出登录' using 'ACCESSIBILITY_ID' with the contextId: '' multiple: false
2016-08-08 14:17:40:067 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=退出登录, INSTANCE=0]
2016-08-08 14:17:40:151 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
2016-08-08 14:17:40:151 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '退出登录' using 'ACCESSIBILITY_ID' with the contextId: '' multiple: false
2016-08-08 14:17:40:152 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=退出登录, INSTANCE=0]
2016-08-08 14:17:40:253 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}

问题 2:

Hybrid 应用,切换到 WEBVIEW 时遇到问题,能正常打印出 contexts,但切换到 WEBVIEW 时抛异常 no such session。

public void toWeb() {
    List<String> handleList = new ArrayList<String>();
    Set<String> set = driver.getContextHandles();
    Iterator<String> it = set.iterator();
    while (it.hasNext()) {
        handleList.add(it.next());
    }
    try {
        driver.context(handleList.get(1));
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

运行异常信息:

no such session
  (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.10.0-327.22.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 21 milliseconds
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'qianchang-PC', ip: '127.0.0.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'
*** Element info: {Using=class name, value=UBI-line-1}
Session ID: dc4862f3-8fd3-4bb4-8db3-84859b272ec1
Driver info: com.test.driver.TestDriver
Capabilities [{platform=LINUX, app=/JenkinsWorkSpace/workspace/Kartor_Android_UBI/apps/kartor_debug_4.9.0.0080.apk, javascriptEnabled=true, appActivity=cn.cst.iov.app.LaunchActivity, browserName=, networkConnectionEnabled=true, noSign=true, desired={newCommandTimeout=300, app=/JenkinsWorkSpace/workspace/Kartor_Android_UBI/apps/kartor_debug_4.9.0.0080.apk, platformName=Android, deviceName=Android, browserName=, noSign=true, unicodeKeyboard=true, resetKeyboard=true, noReset=true}, locationContextEnabled=false, appPackage=cn.cstonline.kartor3, deviceUDID=QMSDU15A24012345, noReset=true, newCommandTimeout=300, platformVersion=5.1.1, databaseEnabled=false, appWaitPackage=cn.cstonline.kartor3, platformName=Android, deviceName=QMSDU15A24012345, appWaitActivity=cn.cst.iov.app.LaunchActivity, webStorageEnabled=false, unicodeKeyboard=true, resetKeyboard=true, warnings={}, takesScreenshot=true}]

Appium Log:

2016-08-08 20:33:28:768 - info: [HTTP] --> GET /wd/hub/session/dc4862f3-8fd3-4bb4-8db3-84859b272ec1/contexts {}
2016-08-08 20:33:28:770 - info: [MJSONWP] Calling AppiumDriver.getContexts() with args: ["dc4862f3-8fd3-4bb4-8db3-8...
2016-08-08 20:33:28:773 - info: [debug] [AndroidDriver] Getting a list of available webviews
2016-08-08 20:33:28:774 - info: [debug] [ADB] Getting connected devices...
2016-08-08 20:33:28:796 - info: [debug] [ADB] 1 device(s) connected
2016-08-08 20:33:28:797 - info: [debug] [ADB] Running /usr/local/androidsdk/platform-tools/adb with args: ["-P",5037,"-s","QMSDU15A24012345","shell","cat","/proc/net/unix"]
2016-08-08 20:33:28:867 - info: [debug] [AndroidDriver] WEBVIEW_11385 mapped to pid 11385
2016-08-08 20:33:28:867 - info: [debug] [AndroidDriver] Getting process name for webview
2016-08-08 20:33:28:869 - info: [debug] [ADB] Getting connected devices...
2016-08-08 20:33:28:890 - info: [debug] [ADB] 1 device(s) connected
2016-08-08 20:33:28:890 - info: [debug] [ADB] Running /usr/local/androidsdk/platform-tools/adb with args: ["-P",5037,"-s","QMSDU15A24012345","shell","ps"]
2016-08-08 20:33:29:026 - info: [debug] [AndroidDriver] Parsed pid: 11385 pkg: cn.cstonline.kartor3!
2016-08-08 20:33:29:026 - info: [debug] [AndroidDriver] from: u0_a158,11385,390,1923008,173416,ffffffff,00000000,R,cn.cstonline.kartor3
2016-08-08 20:33:29:027 - info: [debug] [AndroidDriver] returning process name: cn.cstonline.kartor3
2016-08-08 20:33:29:028 - info: [debug] [AndroidDriver] Found webviews: ["WEBVIEW_cn.cstonline.kartor3"]
2016-08-08 20:33:29:028 - info: [debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_cn.cstonline.kartor3"]
2016-08-08 20:33:29:031 - info: [MJSONWP] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_cn.c...
2016-08-08 20:33:29:036 - info: [HTTP] <-- GET /wd/hub/session/dc4862f3-8fd3-4bb4-8db3-84859b272ec1/contexts 200 265 ms - 117 
2016-08-08 20:33:29:041 - info: [HTTP] --> POST /wd/hub/session/dc4862f3-8fd3-4bb4-8db3-84859b272ec1/context {"name":"WEBVIEW_cn.cstonline.kartor3"}
2016-08-08 20:33:29:042 - info: [MJSONWP] Calling AppiumDriver.setContext() with args: ["WEBVIEW_cn.cstonline.kart...
2016-08-08 20:33:29:045 - info: [debug] [AndroidDriver] Getting a list of available webviews
2016-08-08 20:33:29:045 - info: [debug] [ADB] Getting connected devices...
2016-08-08 20:33:29:066 - info: [debug] [ADB] 1 device(s) connected
2016-08-08 20:33:29:067 - info: [debug] [ADB] Running /usr/local/androidsdk/platform-tools/adb with args: ["-P",5037,"-s","QMSDU15A24012345","shell","cat","/proc/net/unix"]
2016-08-08 20:33:29:132 - info: [debug] [AndroidDriver] WEBVIEW_11385 mapped to pid 11385
2016-08-08 20:33:29:133 - info: [debug] [AndroidDriver] Getting process name for webview
2016-08-08 20:33:29:134 - info: [debug] [ADB] Getting connected devices...
2016-08-08 20:33:29:153 - info: [debug] [ADB] 1 device(s) connected
2016-08-08 20:33:29:154 - info: [debug] [ADB] Running /usr/local/androidsdk/platform-tools/adb with args: ["-P",5037,"-s","QMSDU15A24012345","shell","ps"]
2016-08-08 20:33:29:257 - info: [debug] [AndroidDriver] Parsed pid: 11385 pkg: cn.cstonline.kartor3!
2016-08-08 20:33:29:257 - info: [debug] [AndroidDriver] from: u0_a158,11385,390,1923008,173416,ffffffff,00000000,S,cn.cstonline.kartor3
2016-08-08 20:33:29:262 - info: [debug] [AndroidDriver] returning process name: cn.cstonline.kartor3
2016-08-08 20:33:29:263 - info: [debug] [AndroidDriver] Found webviews: ["WEBVIEW_cn.cstonline.kartor3"]
2016-08-08 20:33:29:264 - info: [debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_cn.cstonline.kartor3"]
2016-08-08 20:33:29:265 - info: [debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_cn.cstonline.kartor3'
2016-08-08 20:33:29:269 - info: [debug] [Chromedriver] Changed state to 'starting'
2016-08-08 20:33:29:296 - info: [Chromedriver] Set chromedriver binary as: /usr/lib/node_modules/.appium_npminstall/node_modules/.appium-chromedriver@2.9.2/chromedriver/linux/chromedriver_64
2016-08-08 20:33:29:298 - info: [Chromedriver] Killing any old chromedrivers, running: pkill -15 -f "/usr/lib/node_modules/.appium_npminstall/node_modules/.appium-chromedriver@2.9.2/chromedriver/linux/chromedriver_64.*--port=9515"
2016-08-08 20:33:29:388 - info: [Chromedriver] No old chromedrivers seemed to exist
2016-08-08 20:33:29:390 - info: [Chromedriver] Spawning chromedriver with: /usr/lib/node_modules/.appium_npminstall/node_modules/.appium-chromedriver@2.9.2/chromedriver/linux/chromedriver_64 --url-base=wd/hub --port=9515 --adb-port=5037
2016-08-08 20:33:29:414 - info: [Chromedriver] [STDOUT] Starting ChromeDriver 2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a) on port 9515
Only local connections are allowed.
2016-08-08 20:33:29:418 - info: [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
2016-08-08 20:33:29:448 - info: [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"stat...
2016-08-08 20:33:29:450 - info: [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"ch...
2016-08-08 20:33:30:526 - info: [JSONWP Proxy] Got response with status 200: {"sessionId":"6e2bbbd8e02b8...
2016-08-08 20:33:30:527 - info: [debug] [Chromedriver] Changed state to 'online'
2016-08-08 20:33:30:528 - info: [MJSONWP] Responding to client with driver.setContext() result: null
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 20 条回复 时间 点赞

我也遇到这个问题了,appium 版本 1.4.x,切换到 WEBVIEW,findELement 的方法就找不到元素,不切换反而可以

#18 楼 @jira win 下可以用这个命令 cmd.exe /C start wmic process where name='chromedriver.exe' call terminate 其实这个百度一下就有很多种方法的

#17 楼 @adfghzhang 怎么杀掉 chromedriver 进程?

#16 楼 @pcc426 可以试试切换 WEBVIEW 之前可以先杀掉 chromedriver 进程

#15 楼 @lirenjun_2016 同遇到此问题,搞了 2 天了,求解答😥

问题 2 我也遇到了,在 4.4 系统上面不会有问题,在 5.1 系统上面,切换到 WEBVIEW 时遇到问题,能正常打印出 contexts,但切换到 WEBVIEW 时抛异常 no such session。

#12 楼 @chungeguo 设置这个 desired_caps["autoWebview"] = True 就不用在代码里面切换了吗?是 Appium 1.5.3 还是 Appium 1.4 的版本呀?

#11 楼 @lihuazhang java 还得切,不然没反应 chromedriver 都不启动

#11 楼 @lihuazhang 我是一直都没有切换过,python 版本的 ios,android,都不切换

#2 楼 @chungeguo 不用切 context 了啊?

#1 楼 @lihuazhang 替换了一个低版本的 chromedriver,执行就没有问题了,这好诡异。

#8 楼 @kesha0 我用纯命令行安装的,保证 sdk 安装上比较容易,但是 aapt 要需要 libc++ 就需要自己去编译。先下载 linux 版的 sdk 解压,然后到 tools 目录执行该命令安装 sdk 中的 build-tools,platform-tools,tools。中间如果提示差什么就安装什么

./android update sdk -u -t 1,2,3

想问下 centos 下怎么安装安卓 sdk..

#6 楼 @adfghzhang 我是 python,没有切换

#5 楼 @chungeguo 刚刚试验了一下不切 web 连 chromedriver 都不会起,难道 python 跟 java 不一样?

而且真对 Hybrid 应用,现在不用切换 webview 啦

self.driver.find_element_by_android_uiautomator('text("我知道啦")').click()

#2 楼 @chungeguo 针对 native 元素 text 属性现在能用什么方法来找元素呢

1.5.3 版本,driver.findElementByAccessibilityId("退出登录") 这个方法是真对 web 元素来使用的,而且对应的是 content-desc

no such session 这个感觉么啥问题啊

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