Appium appium swipe 的问题

愤怒的小鸟 · 2017年11月28日 · 最后由 etar 回复于 2018年09月04日 · 1791 次阅读

appium java-client 5.0 以后移除了 swipe 方法,也就是无法使用 driver.swipe() 了,有什么替代的方法吗

共收到 5 条回复 时间 点赞
1楼 已删除

不好意思 回复了 python 的处理方法

希望有用

import io.appium.java_client.TouchAction;

/**
     * description:定位到元素坐标,然后进行滑动操作
     * id: 要操作的元素     *
     * moveX:从起始x位置需要移动的距离(移动到beginX+moveX的位置)
     * moveY:从起始y位置需要移动的距离(移动到beginY+moveY的位置)
     */
    public void swipeToPosition(String id,int moveX,int moveY){

        WebElement elem = driver.findElement(By.id(id));
        Point start = elem.getLocation();
        TouchAction ta = new TouchAction(driver);

        int startX = start.x;
        int startY = start.y;
        // 获取控件坐标轴差
        Dimension q = elem.getSize();
        int x = q.getWidth();
        int y = q.getHeight();
        // 计算出控件结束坐标
        int endX = x + startX;
        int endY = y + startY;

        // 计算中间点坐标
        int centreX = (endX + startX) / 2;
        int centreY = (endY + startY) / 2;
        ta.press(centreX,centreY).moveTo(moveX,moveY).release().perform();
    }

    /**
     * 通过传坐标来执行滑动操作
     * start(startX,startY)
     * end(endX,endY)
     *
     * @param startX 起始X坐标
     * @param startY 起始Y坐标
     * @param endX   终点X坐标
     * @param endY   终点Y坐标
     */
    public void swipe(int startX, int startY, int endX, int endY) {
        Map<String, Object> params = new HashMap<>();
        params.put("duration", 0.5);
        params.put("fromX", startX);
        params.put("fromY", startY);
        params.put("toX", endX);
        params.put("toY", endY);
//        params.put("element", swipeView.getId());
        jse.executeScript("mobile: dragFromToForDuration", params);
    }

    /**
     * 通过滑动元素和滑动方向来执行滑动操作
     *
     * @param swipeView 动元素
     * @param toSwipe   滑动方向
     *                  1. up
     *                  2. down
     *                  3. left
     *                  4. right
     */
    public void swipe(MobileElement swipeView, String toSwipe) {
        Map<String, Object> params = new HashMap<>();
        params.put("direction", toSwipe);
        params.put("element", swipeView.getId());
        params.put("duration", 500);
        jse.executeScript("mobile: swipe", params);
    }

这个是经过验证的,有效

java-client 5.0 并不只有 swipe 无效了,有很多 api 无效了,如果楼主升级完了,可以发个贴子全部归类出来就完美了

jse 是什么呢

simple 回复

这种方式有时候会报错,请问有什么方法解决吗?“org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: The swipe did not complete successfully

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