【问题标题】:How to zoom in a mobile web using Appium如何使用 Appium 放大移动网络
【发布时间】:2025-12-14 11:05:02
【问题描述】:

我的项目中有一个场景,我需要使用 Appium 自动化执行放大操作 Android Mobile web。我使用了以下方法。

try {
        Dimension size = driver.manage().window().getSize();
        int x0 = (int) (size.getWidth()*0.5);
        int y0 = (int) (size.getHeight()*0.5);
        System.out.println(x0+" "+y0);
        driver.zoom(100, 500);
        reportStep("The Application is zoomed.", "PASS");
    } catch (Exception e) {
        e.printStackTrace();
        reportStep("The Application could not be zoomed.", "FAIL");
    }

但是这种方法在移动应用程序中运行良好,但在移动网络中不起作用。 是否有任何特定的方法或替代解决方法来处理移动网络缩放?

我正在使用 Appium Java 客户端 5.0.4 版和 Selenium 3.6.0 版。

Zoom 和 Pinch 方法在最新版本中不可用。

【问题讨论】:

    标签: appium mobile-website


    【解决方案1】:

    没有zoom 方法的例子不多,但是使用MultiTouchAction 类和TouchAction 类应该可以如here 所示工作。请注意,在该线程中建议使用 moveToWebElement 和偏移 x,y 坐标而不是绝对坐标。

        driver = new AndroidDriver(new URL(nodeURL), capability);
    
        TouchAction  touchAction = new TouchAction(driver);
        MultiTouchAction  multiTouchAction = new MultiTouchAction(driver);
    
        WebElement search = (WebElement)   driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.google.android.apps.maps:id/search_omnibox_container\")"));
         touchAction .tap(search).perform();
    
            int leftX = search.getLocation().getX();
            int rightX = search.getSize().getWidth() + leftX;
    
    
            int upperY = search.getLocation().getY();
            int lowerY = search.getSize().getHeight() + upperY;
    
           a1 = touchAction.press(search,x1,y1).waitAction(2000).moveTo(search,x3,y1).release();
           a2 = touchAction.press(search,x2,y1).waitAction(2000).moveTo(search,x4,y1).release();
    
           multiTouchAction.add(a1).add(a2).perform();
    

    另外,查看here 以获得appium MultiTouch 和here 以将偏移x,y 坐标传递给moveTo

    【讨论】:

    • 以上代码不适用于使用 Appium 缩放移动网络浏览器。我收到以下错误。方法还没有实现这里是代码 sn -p MultiTouchAction multiTouch = new MultiTouchAction(driver); TouchAction action0 = new TouchAction(driver).tap(element); multiTouch.add(action0).perform();实际上,我们正在尝试使用 linktext 在移动网络浏览器中单击链接,但文本非常小,因此单击无法正常工作。所以我们正在尝试缩放浏览器并点击它。
    • TouchAction 和 MultiTouchAction 仅适用于原生视图。
    最近更新 更多