【问题标题】:How to swipe till element is found in appium with using loop如何使用循环滑动直到在appium中找到元素
【发布时间】:2020-10-19 15:33:48
【问题描述】:

我可以滑动直到找到元素,然后我得到 NoSuchElementException。

我写了以下代码。

int startX=(int) (dimension.width*0.5);
        int startY=(int) (dimension.height*0.8);

        int endX=(int) (dimension.width*0.2);
        int endY=(int) (dimension.width*0.2);

        TouchAction touch=new TouchAction(driver);
        boolean b=false;
        while (true) {
            touch.press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
            .moveTo(PointOption.point(endX, endY)).release().perform();
            Thread.sleep(5000);
            try {
                b=driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).isDisplayed();
                if(b) {
                    b=true;
                    break;
                }
                else {
                    continue;
                }
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).click();

请帮忙。谢谢

【问题讨论】:

    标签: java android appium appium-android


    【解决方案1】:

    使用基于 UIAutomator 的定位器会更可靠,因为您显然有一个可滚动的视图:

    MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().scrollable(true))" +
             ".scrollIntoView(new UiSelector().textContains(\"Sweep\"))"));
    

    它不仅会滚动到元素,而且确保它在视口区域内并且能够进行交互。

    【讨论】:

    • 我知道这种方式,但我一直在寻找其他方式。我使用了我发布的类似代码,用于滚动 Web 应用程序,直到找到我正在寻找的元素
    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2021-07-03
    • 1970-01-01
    • 2019-09-24
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多