Appium (已解决) 关于长按 driver.long_press_keycode (),报错

付兰 · March 01, 2016 · Last by 悦锦软件系统(上海)有限公司 replied at March 02, 2016 · 1587 hits

网上查找了一下虚拟键点击和长按的方法,目前使用以下点击都可以:
driver.keyevent(‘4’)
或者 driver.press_ keycode(‘4’)
但是在使用长按 driver.long_press_keycode(‘3’) 时出现错误,代码如下:

#coding:utf-8
from appium import webdriver
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '0123456789ABCDEF'
desired_caps['appPackage'] = 'com.android.mms'
desired_caps['appActivity'] = '.ui.ConversationList'
dr = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
dr.find_element_by_class_name("android.widget.ImageButton").click()
dr.find_element_by_xpath("//android.widget.ListView/android.widget.LinearLayout[2]").click()
dr.press_keycode("4")
dr.long_press_keycode('3')

然后报错的信息如下:

目前解决方法就是去掉引号:driver.long_press_keycode(3)

共收到 12 条回复 时间 点赞

APPIUM 版本多少?

#2 楼 @a3096556718 试下直接传 int 的 3 ,不要传字符串?


代码里用了强制转换,看你的报错是强制转换时出错了。

付兰 #5 · March 01, 2016 Author

#3 楼 @chenhengjie123 可以了哦,谢谢你!!自己琢磨一上午都不懂,幸好遇到你了,嘿嘿

#4 楼 @chenhengjie123 还是恒姐分析的透彻,虽然知道直接 self.driver.long_press_keycode(3) 就可以了。是不是顺便让(Appium Python API 中文版 By-HZJ https://testerhome.com/topics/3711)也更新下用法。

adb shell input keyevent 也可以支持 longpress 虚拟按键了,亲测可用,需要先升级到版本为 1.0.32 的 adb,命令说明如下:
adb shell input
Usage: input [] [...]

The sources are:
mouse
keyboard
joystick
touchnavigation
touchpad
trackball
stylus
dpad
touchscreen
gamepad

The commands and default sources are:
text (Default: touchscreen)
keyevent [--longpress] ... (Default: keyboard)

实例:
adb shell input keyevent --longpress KEYCODE_MENU

#6 楼 @t880216t 我更新一下

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up