前言

未加'automationName': 'Uiautomator2'参数使用Uiautomator可以正常使用driver.keyevent()方法,使用Uiautomator2时driver.keyevent()方法报错:

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: 404 - undefined

python V3.6

selenium V2.53.6

Appium-Python-Client V0.26

appium server V1.7.1

Uiautomator2

遇到问题

未加'automationName': 'Uiautomator2'参数使用Uiautomator可以正常使用driver.keyevent()方法,使用Uiautomator2时driver.keyevent()方法报错:

  1. from appium import webdriver
  2. import time
  3. desired_caps = {
  4. "platformName": "Android",
  5. "deviceName": "emulator-5554", # 设备名称
  6. "platformVersion": "5.1.1", # android系统版本号
  7. "appPackage": "com.yipiao", # app包名
  8. "appActivity": "com.yipiao.activity.LaunchActivity", # 启动launch Activity
  9. "noReset": True, # 不清空数据
  10. 'automationName': 'Uiautomator2' # toast 必须用Uiautomator2
  11. }
  12. driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
  13. time.sleep(10) # 启动时间等待,放长一点
  14. # Uiautomator2使用keyevent事件报错
  15. driver.keyevent(4) # back事件 code 4

调用keyevent方法,执行back事件,对应的code值是4,发现报错

  1. Traceback (most recent call last):
  2. File "D:/apptest/ke10/t_start.py", line 25, in <module>
  3. driver.keyevent(4) # back事件 code 4
  4. File "E:\python36\lib\site-packages\appium\webdriver\webdriver.py", line 411, in keyevent
  5. self.execute(Command.KEY_EVENT, data)
  6. File "E:\python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
  7. self.error_handler.check_response(response)
  8. File "E:\python36\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
  9. raise wde
  10. File "E:\python36\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
  11. super(MobileErrorHandler, self).check_response(response)
  12. File "E:\python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
  13. raise exception_class(message, screen, stacktrace)
  14. selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: 404 - undefined

主要原因是Uiautomator2上对keyevent不支持了,用driver.press_keycode()方法可以解决

press_keycode 和keyevent

查看press_keycode 和keyevent 源码,发现这2个方法没啥区别,keyevent上面有个备注Needed for Selendroid,可能是老版本里面的功能。

新版本用Uiautomator2可以使用 press_keycode 方法

  1. # Needed for Selendroid
  2. def keyevent(self, keycode, metastate=None):
  3. """Sends a keycode to the device. Android only. Possible keycodes can be
  4. found in http://developer.android.com/reference/android/view/KeyEvent.html.
  5. :Args:
  6. - keycode - the keycode to be sent to the device
  7. - metastate - meta information about the keycode being sent
  8. """
  9. data = {
  10. 'keycode': keycode,
  11. }
  12. if metastate is not None:
  13. data['metastate'] = metastate
  14. self.execute(Command.KEY_EVENT, data)
  15. return self
  16. def press_keycode(self, keycode, metastate=None):
  17. """Sends a keycode to the device. Android only. Possible keycodes can be
  18. found in http://developer.android.com/reference/android/view/KeyEvent.html.
  19. :Args:
  20. - keycode - the keycode to be sent to the device
  21. - metastate - meta information about the keycode being sent
  22. """
  23. data = {
  24. 'keycode': keycode,
  25. }
  26. if metastate is not None:
  27. data['metastate'] = metastate
  28. self.execute(Command.PRESS_KEYCODE, data)
  29. return self

使用 press_keycode 替换 keyevent即可解决问题

  1. from appium import webdriver
  2. import time
  3. desired_caps = {
  4. "platformName": "Android",
  5. "deviceName": "emulator-5554", # 设备名称
  6. "platformVersion": "5.1.1", # android系统版本号
  7. "appPackage": "com.yipiao", # app包名
  8. "appActivity": "com.yipiao.activity.LaunchActivity", # 启动launch Activity
  9. "noReset": True, # 不清空数据
  10. 'automationName': 'Uiautomator2' # toast 必须用Uiautomator2
  11. }
  12. driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
  13. time.sleep(10) # 启动时间等待,放长一点
  14. # Uiautomator2使用keyevent事件报错
  15. # driver.keyevent(4) # back事件 code 4
  16. # Uiautomator2使用press_keycode解决
  17. driver.press_keycode(4)

keycode相关方法

  • driver.press_keycode(4) #发送keycode,功能:按键 Uiautomator2使用keyevent事件报错
  • driver.keyevent(4) #发送keycode,功能:按键,与press_keycode无区别 Uiautomator2正常使用
  • driver.hide_keyboard() #iOS使用key_name隐藏,安卓不使用参数,功能:隐藏键盘
  • driver.long_press_keycode(4) #发送keycode,功能:长按键

appiumQQ交流群:779429633

appium+python自动化64-使用Uiautomator2执行driver.keyevent()方法报错解决的更多相关文章

  1. appium+python自动化63-使用Uiautomator2报错问题解决

    前言 appium desktop V1.7.1版本使用命令行版本启动appium后,使用Uiautomator2定位toast信息报错:appium-uiautomator2-server-v0.3 ...

  2. appium+python自动化38-adb shell按键操作(input keyevent)

    前言 接着上篇介绍input里面的按键操作keyevent事件,发送手机上常用的一些按键操作 keyevent 1.keyevent事件有一张对应的表,可以直接发送对应的数字,也可以方式字符串,如下两 ...

  3. appium+python自动化-adb shell按键操作(input keyevent)

    前言 接着上篇介绍input里面的按键操作keyevent事件,发送手机上常用的一些按键操作 keyevent 1.keyevent事件有一张对应的表,可以直接发送对应的数字,也可以方式字符串,如下两 ...

  4. vue中使用ts后,父组件获取执行子组件方法报错问题

    一.问题产生背景: 子组件的一个方法: update () { this.$nextTick(() => { this.ul_slots.forEach((ul, cur_slots_index ...

  5. Appium+python自动化8-Appium Python API

    Appium+python自动化8-AppiumPython API   前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...

  6. appium+python自动化52-多点触控MultiAction

    前言 MultiAction是针对多点触控操作的,是TouchAction的一个补充模块 TouchAction用法参考前面的一篇:appium+python自动化33-TouchAction 多点触 ...

  7. appium+python自动化61-中文输入乱码问题解决

    前言 在夜神模拟器上输入中文,发现是乱码,将unicodeKeyboard和resetKeyboard参数设置为True了,发现还是没法解决. 打开手机设置语言和输入法,发现找不到Appium And ...

  8. Appium+python自动化20-查看iOS上app元素属性

    前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素 Mac版的appium1.6的 ...

  9. Appium+python自动化20-查看iOS上app元素属性【转载】

    前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素Mac版的appium1.6的版 ...

随机推荐

  1. react一些问题

    一.死循环 1.问题描述 function handleClick() { this.setState({count: ++this.state.count}); console.log(" ...

  2. 【计算机视觉基础】IPM

    IPM code #if 0 void xyp2ipmp(cv::Mat& xyp, cv::Mat& ipmp, cv::Mat& xylim, Size& sz){ ...

  3. Centos7中rc.local设置springboot项目开机自启动

    在Centos7下,rc.local文件,开机默认是不执行的 1.进入rc.local中 路径如下图

  4. c#之break和continue的区别

    break:跳出循环,执行循环外的语句: continue:跳出此次循环,进入下一次循环:

  5. 湖南省第十三届大学生计算机程序设计竞赛 Football Training Camp 贪心

    2007: Football Training Camp[原创-转载请说明] Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 1 ...

  6. window安装mysql8.0解决大部分客户端无法连接问题登陆问题

    https://blog.csdn.net/u013308810/article/details/80114021

  7. json对象转js对象

    json数据: { "YD1": 0, "YD2": 0, "YD3": 0, "YD4": 0, "YD5& ...

  8. [转帖]Zookeeper入门看这篇就够了

    Zookeeper入门看这篇就够了 https://my.oschina.net/u/3796575/blog/1845035 Zookeeper是什么 官方文档上这么解释zookeeper,它是一个 ...

  9. 【leetcode-97 动态规划】 交错字符串

    (1过,调试很久) 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...

  10. 开启Telnet服务

    在Win7系统中安装和启动Telnet服务非常简单:依次点击“开始”→“控制面板”→“程序”,“在程序和功能”找到并点击“打开或关闭Windows功能”进入Windows 功能设置对话框.找到并勾选“ ...