Appium 如何实现滑动或者滚动,以选择到楼层?楼层元素应如何定位?

Magicchn · 2018年07月10日 · 最后由 萧帅 回复于 2018年09月18日 · 2265 次阅读

两个问题,求助各位

  • 问题 1,APP 上该页面是可以上下滑动,选择不同楼层,封装了一个向上的滑动方法,但是每次运行脚本是,都不执行滑动操作,该如何实现滑动?
  • 问题 2 ,页面楼层元素定位困难,inspector 虽然给出了 xpath 路径,但提示不可靠,还有什么方法可以实现定位楼层元素? 环境信息:appium+pyhton # 封装函数
  • def swipeup_slight(self, t=1500, n=1):
    • logging.info('====向上滑动====')
    • l = self.get_size()
    • x1 = int(l[0] * 0.5)
    • y1 = int(l[0] * 0.8)
    • y2 = int(l[0] * 0.7)
    • for i in range(n):
    • self.swipe(x1, y1, x1, y2, t)

滑动调用及报错信息

appium 日志信息

[W3C] Calling AppiumDriver.performTouch() with args: [[{"action":"press","options":{"x":540,"y":972}},{"action":"wait","options":{"ms":1500}},{"action":"moveTo","options":{"x":540,"y":108}},{"action":"release","options":{}}],"ba3c2b7a-137a-4a6b-aad6-cf3d9853f79e"]
[JSONWP Proxy] Proxying [POST /touch/perform] to [POST http://localhost:8200/wd/hub/session/1a3cb740-ee8-88ed22f51de8/touch/perform] with body: {"startX":540,"startY":972,"endX":540,"endY":108,"steps":42}
[W3C] Encountered internal error running command: {"w3cStatus":400,"jsonwp":{"sessionId":"1a3cb740-eae8-47ce-b5e8-88ed22f51de8","status":13,"value":"Swipe did not complete successfully"}} ProxyRequestError: Could not proxy command to remote server. Original error: 500 - {"sessionId":"1a3cb740-eae8-47ce-b5e8-88ed22f51de8","status":13,"value":"Swipe did not complete successfully"}
[W3C] at JWProxy.proxy$ (C:\Program Files (x86)\Appium\resources\app\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:152:13)
[W3C] at tryCatch (C:\Program Files (x86)\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:67:40)
[W3C] at GeneratorFunctionPrototype.invoke as _invoke
[W3C] at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
[W3C] at GeneratorFunctionPrototype.invoke (C:\Program Files (x86)\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[W3C] at
[MJSONWP] Matched JSONWP error code 13 to UnknownError

相关信息截图

方式一,uiautomatorviewer 定位显示,不能定位到楼层选择元素

方式二,appium inspector 定位显示,可以定位到但 xpath 路径可能有问题,inspector 已给出提示

上下文环境

最佳回复

我能想到的是实现一个专门的方法来滑动这个控件。计算好滑动的初始位置,终止位置,然后如果要滑动到第几层,就循环几次。你问题的报错是不是因为坐标计算错误了?
不知道有没有更好的办法了。

共收到 9 条回复 时间 点赞

我能想到的是实现一个专门的方法来滑动这个控件。计算好滑动的初始位置,终止位置,然后如果要滑动到第几层,就循环几次。你问题的报错是不是因为坐标计算错误了?
不知道有没有更好的办法了。

def swipe(self, start_x, start_y, end_x, end_y, count=1):
    """
    滑动
    :param start_x: 起始x
    :param start_y: 起始y
    :param end_x:   结束x
    :param end_y:   结束y
    :param count:   次数
    """
    # self.driver.swipe(start_x, start_y, end_x, end_y)
    for i in range(count):
        AdbUtils.exe_adb(" shell input swipe %s %s %s %s 300" % (start_x, start_y, end_x, end_y))
        time.sleep(2)

我是用的 adb 命令滑动 自带的 swipe 不知道为什么 反正是没用~

雨夜狂奔 回复

感谢回复,我没有使用坐标来定位,只是滑动方法中用到了屏幕比例

wtnhz 回复

你说的这个问题我之前也遇到过,有两个思路,因为是我遇到过的并且解决了:
1,很有可能是时间设置的太短了,建议你可以把时间改大之后再试,
2,或者是引用不对,如果是自己重新封装的滑动,引用时,直接 self.swipe() 即可,不用 self.driver.swipe()

先谢过,希望不要被淹没了,哪位高人指点下

我暂时用每次上滑一栏,然后定义好值对应上滑几次。以此达到选择的目的

仅楼主可见
安涛 回复

目前已经解决,是通过计算坐标、滑动固定距离解决的。

Magicchn 回复

楼主是否可以贴下这段滑动或滚动实现楼层元素定位的代码 参考下 谢谢!!!!

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