How to scroll until found exact element in appium for android?

I’m using below method for page scroll up:

public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width * anchorPercentage);
    int startPoint = (int) (size.height * startPercentage);
    int endPoint = (int) (size.height * finalPercentage);
    new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(anchor, endPoint).release().perform();
  }

My questions are:

  1. I just want to scroll up the page until found the exact element, how I con do it?
  2. How to scroll down?
1 Like

@Al_Imran, I suspect you are farther along than you question suggests, so please forgive me if my answer provides information that you already know

You can scroll down by reversing startPoint and endPoint

You can create a loop around scroll up or scroll down that consists of a search for the object, and a swipe if it fails.

If this doesn’t make sense or you are already doing something similar and it’s not working, or I interpreted your question incorrectly, please continue the conversation.

1 Like

@willosser thanks for your ans, 1 no question is solved by using while loop, but scroll down is not working by reversing startPoint and endPoint, is there any other way please ?

Solution of 1 no question:

Boolean isFoundElement;
By myElement = By.id("progressBar");

isFoundElement = driver.findElements(myElement).size() > 0;
        while (isFoundElement == false){
            swipeVertical((AppiumDriver) driver,0.9,0.1,0.5,2000);
            isFoundElement = driver.findElements(myElement).size() > 0;
        }
        driver.findElement(myElement).click();
4 Likes

If your screen is split into scrollable and non-scrollable areas, that might be your problem. I’ve solved that problem by finding the (x,y) coordinates of the window and making certain my start and end points are inside that boundary

Hi,

Is it possible to swipe to an exact element by name? similar to scrollToExact()…

Please help

I have solved the problem by using a while loop and a boolean

boolean isFoundTheElement = driver.findElements(expectedElement).size() > 0;
        while (isFoundTheElement == false){
            swipeVertical((AppiumDriver) driver,0.9,0.1,0.5,2000);
            isFoundTheElement  = driver.findElements(expectedElement).size() > 0;
        }
1 Like

Hi Imran swipe vertical is working, but if there are multiple horizontal scrolls in page and it’s text view for second element. How to get second element.

Have you tried the method for horizontal swiping ?

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.height * anchorPercentage);
        int startPoint = (int) (size.width * startPercentage);
        int endPoint = (int) (size.width * finalPercentage);
        new TouchAction(driver).press(startPoint, anchor).waitAction(Duration.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
    }

Yes i have used your code it scrolls ,but not from particular point of textView . For Example: From attached screenshot - Exactly from Burgers category horizontal scroll how to perform?

Had used your code and also :
Dimension dsize=driver.manage().window().getSize();
int startx =(int)(dsize.width0.80);
int endx =(int)(dsize.width
0.10);
int starty =(int)(dsize.height/2);
driver.swipe(startx, starty, endx, 0, 2000);