简体   繁体   中英

How to swipe till element is found in appium with using loop

I am able to swipe till element is found but then I am getting NoSuchElementException.

I have written following code.

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

Please help. Thanks在此处输入图片说明

It would be more reliable to use UIAutomator-based locator, cause you obviously have a scrollable view:

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

It will not only scroll to element, but make sure it is within view port area and able for interations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM