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

1.2k Views Asked by At

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. Thanksenter image description here

1

There are 1 best solutions below

1
On

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.