如何移动到元素并在移动应用程序中按下

How to move to element and press on in mob app

如何使用 xpath 移动到屏幕上不可见的元素,然后在移动应用程序中单击它? 该元素位于底部,需要向上滑动

这是一个滚动和点击的小例子:

public void scrollDown() {
    Dimension size = driver.manage().window().getSize();
    int startHeight = (int) (size.height * 0.5);
    int startWidth = (int) (size.width * 0.5);
    int endHeight = (int) (size.height * 0.25);
    int endWidth = (int) (size.width * 0.5);

    new TouchAction(driver).press(PointOption.point(startWidth, startHeight)).waitAction().moveTo(PointOption.point(endWidth, endHeight)).release().perform();
}


public void scrollUntilElementIsVisible(WebElement element) {
    try {
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        boolean flag = element.isDisplayed();
        while (!flag) {
            scrollDown();
        }
        element.click();
    } catch (NoSuchElementException e) {
        System.out.println(e.toString());
    } finally {
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
}

您需要使用findElementByAndroidUIAutomator方法。它滚动到您的特定元素,当它找到它时,您可以对该元素调用 click()

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + Element_text + "\").instance(0))");
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Element_text\")").click();

我用:

TouchAction ta = new TouchAction(driver);
ta.press(button).moveTo(x, y).release().perform();