如何在appium中到达滚动条的末尾?

问题描述 投票:1回答:1

我正在尝试自动滚动水平条,其中条形图的元素是动态的,并且是从API获取的。

有没有办法在appium中自动化它?

java selenium selenium-webdriver appium appium-android
1个回答
3
投票

如果页面底部有任何元素或文本,则可以使用UiAutomator2。

如果您使用appium,请添加所需的功能'UiAutomator2'。

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

如果你有元素的id,现在使用下面的函数

 public void scrollByID(String Id, int index) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\""+Id+"\").instance("+index+"));")); 

        } catch (Exception e) {
           e.printStackTrace();
        }
    }

如果您有元素的文本,请使用此选项。

public void scrollByText(String menuText) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + menuText + "\").instance(0));")); 
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

如果你不知道botton中的任何元素,那么你必须使用屏幕尺寸

public void scrollToBottom() {

      int  x = driver.manage().window().getSize().width / 2;
      int start_y = (int) (driver.manage().window().getSize().height * 0.2);
      int end_y = (int) (driver.manage().window().getSize().height * 0.8);
        TouchAction dragNDrop = new TouchAction(driver)
                        .press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                        .moveTo(PointOption.point(x, end_y))
                        .release();
        dragNDrop.perform();
    }
© www.soinside.com 2019 - 2024. All rights reserved.