appium+python自動化64-使用Uiautomator2執行driver.keyevent()方法報錯解決

前言

未加'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 - undefinedhtml

python V3.6
selenium V2.53.6
Appium-Python-Client V0.26
appium server V1.7.1
Uiautomator2python

遇到問題

未加'automationName': 'Uiautomator2'參數使用Uiautomator能夠正常使用driver.keyevent()方法,使用Uiautomator2時driver.keyevent()方法報錯:android

from appium import webdriver
import time

desired_caps = {
                "platformName": "Android",
                "deviceName": "emulator-5554",     # 設備名稱
                "platformVersion": "5.1.1",     # android系統版本號
                "appPackage": "com.yipiao",   # app包名
                "appActivity": "com.yipiao.activity.LaunchActivity",   # 啓動launch Activity
                "noReset": True,  # 不清空數據
                'automationName': 'Uiautomator2'  # toast 必須用Uiautomator2
                }
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

time.sleep(10)    # 啓動時間等待,放長一點


# Uiautomator2使用keyevent事件報錯
driver.keyevent(4)    # back事件  code 4

調用keyevent方法,執行back事件,對應的code值是4,發現報錯web

Traceback (most recent call last):
  File "D:/apptest/ke10/t_start.py", line 25, in <module>
    driver.keyevent(4)  # back事件  code 4
  File "E:\python36\lib\site-packages\appium\webdriver\webdriver.py", line 411, in keyevent
    self.execute(Command.KEY_EVENT, data)
  File "E:\python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "E:\python36\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
    raise wde
  File "E:\python36\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
    super(MobileErrorHandler, self).check_response(response)
  File "E:\python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
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()方法能夠解決app

press_keycode 和keyevent

查看press_keycode 和keyevent 源碼,發現這2個方法沒啥區別,keyevent上面有個備註Needed for Selendroid,多是老版本里面的功能。
新版本用Uiautomator2可使用 press_keycode 方法ide

# Needed for Selendroid
    def keyevent(self, keycode, metastate=None):
        """Sends a keycode to the device. Android only. Possible keycodes can be
        found in http://developer.android.com/reference/android/view/KeyEvent.html.

        :Args:
         - keycode - the keycode to be sent to the device
         - metastate - meta information about the keycode being sent
        """
        data = {
            'keycode': keycode,
        }
        if metastate is not None:
            data['metastate'] = metastate
        self.execute(Command.KEY_EVENT, data)
        return self

    def press_keycode(self, keycode, metastate=None):
        """Sends a keycode to the device. Android only. Possible keycodes can be
        found in http://developer.android.com/reference/android/view/KeyEvent.html.

        :Args:
         - keycode - the keycode to be sent to the device
         - metastate - meta information about the keycode being sent
        """
        data = {
            'keycode': keycode,
        }
        if metastate is not None:
            data['metastate'] = metastate
        self.execute(Command.PRESS_KEYCODE, data)
        return self

使用 press_keycode 替換 keyevent便可解決問題code

from appium import webdriver
import time

desired_caps = {
                "platformName": "Android",
                "deviceName": "emulator-5554",     # 設備名稱
                "platformVersion": "5.1.1",     # android系統版本號
                "appPackage": "com.yipiao",   # app包名
                "appActivity": "com.yipiao.activity.LaunchActivity",   # 啓動launch Activity
                "noReset": True,  # 不清空數據
                'automationName': 'Uiautomator2'  # toast 必須用Uiautomator2
                }
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

time.sleep(10)    # 啓動時間等待,放長一點


# Uiautomator2使用keyevent事件報錯
# driver.keyevent(4)    # back事件  code 4

# Uiautomator2使用press_keycode解決
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交流羣:779429633orm

相關文章
相關標籤/搜索