【问题标题】:Scrolling down to find element with text not working in android emulator向下滚动以查找文本在 android 模拟器中不起作用的元素
【发布时间】:2020-03-18 17:34:16
【问题描述】:

我正在使用 Appium 桌面服务器 (v 1.15) 以及 java-client 作为 7.3.0 和 selenium-server 3.141.59。

在我的 android 模拟器中,我需要向下滚动以查看文本并在找到文本后停止滚动。 我尝试使用以下内容:

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\""+text+"\"));");

driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + containedText + "\").instance(0))"));

where driver is AndroidDriver<MobileElement>

我面临的问题是即使正确找到了文本,滚动也不会停止。

请帮忙!

【问题讨论】:

    标签: java selenium android-emulator appium


    【解决方案1】:

    这是我使用的自定义滚动方法:

    public void scroll(MobileElement anchorElement, double startPointOffsetPercentage, double endpointOffsetPercentage) {
        //Determines the type of driver used for the gesture (i.e. AndroidDriver, iOSDriver, etc) 
        TouchAction scroll = new TouchAction((MobileDriver) anchorElement.getWrappedDriver());
    
        //Gets your anchor element size to use as reference for the scroll
        Dimension size = anchorElement.getSize();
        int anchor = anchorElement.getCenter().getX();
        int startPoint = (int) (size.height * startPointOffsetPercentage);
        int endPoint = (int) (size.height * endpointOffsetPercentage);
    
        //Performs the gesture using your anchor element as reference for the action,the start and end offsets as the amount to scroll. Duration can be set to smaller to flick instead of scroll. 
        scroll.press(PointOption.point(anchor, startPoint)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(anchor, endPoint)).release().perform();
    
    }
    

    我将其用作“滚动到元素”的自定义方法:

     public void scrollToElement(String elementId, MobileElement scrollableElement) {
        MobileDriver driver = (MobileDriver) scrollableElement.getWrappedDriver();
        try {
            for (int i = 0; i <= 100; i++) {
                if (driver.findElements(By.id(elementId)).isEmpty() || !driver.findElement(By.id(elementId)).isDisplayed()) {
                    scroll(scrollableElement, 0.5, 0.1);
                }
            }
        } catch (NoSuchElementException ignored) {
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 resource-id 而不是 scrollable(true) 尝试以下操作,因为它并不适用于所有情况,resource-id 应该是您要滚动到的父元素的 id:

          driver.findElement(MobileBy.AndroidUIAutomator(
                      "new UiScrollable(new UiSelector().resourceId(\"com.androidsample.generalstore:id/rvProductList\")).scrollIntoView(text(\""+ item + "\"));"));
      

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 2023-04-04
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 2020-01-09
        • 2019-11-26
        • 1970-01-01
        相关资源
        最近更新 更多