首页 > 解决方案 > 我无法在我的移动应用自动化框架中滚动和选择 Appium android 中的元素。?

问题描述

这是我的代码快照。此代码将选择元素,但不会滚动到确切的元素并选择值。请帮我修改一下?

public void applyBedTimefromdate() throws  InterruptedException {
MobileElement element = (MobileElement) driver
                .findElementsByAndroidUIAutomator(
                        "new UiScrollable(new UiSelector().resourceId(\"com.hdc:id/hour\")).getChildByText("
                                + "new UiSelector().className(\"android.view.View[@index='0']\"), \"3\"));");
        element.click();
}

标签: androidmobileui-automationappium-androidandroid-uiautomator

解决方案


我们可以尝试使用 UiAutomator2 按文本滚动。

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

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

现在试试下面的代码。

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();
        }
    }

如果您有元素的 id(资源 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();
        }
    }

推荐阅读