Scrolling to element in android app using java

You can watch this video : https://youtu.be/uiUVUZwBN84

Hi @Aleksei, any idea if this works on horizontal scroll? I tried it on a horizontal scroll where it didn’t work. But the same logic worked when I used it on a vertical scroll.

While trying to use it on a horizontal scroll, it tried to scroll it vertically rather than horizontally.

Thanks,
Anish

@anish10110 add your code to understand the issue.

I have textview element (Not list view item) which is not visible . It is visible only when i swipe/scroll up. I am unable to click it as it is not visible. How can i scroll up and click on that item
Please explain me how can i scroll to that item and click it.
Appium 1.7.1
java 1.8.0
My framework is Serenity based POM model
I am using below code :
@AndroidFindBy(uiAutomator=“new UiScrollable(new UiSelector()).scrollIntoView(text(” NPO3"));")
private WebElement scrollUntillDisneyChannel;

scrollUntillDisneyChannel.click();

2 Likes

Hi, @Aleksei
I am familiarising myself with UiSelector and UiScrollable and at this point it seems like this approach is great when you know some element is there for sure. When you are not sure about the state (or content of ListView), you are just forced to scroll and check using TouchAction which is tedious. I wonder if there is any trick available to scroll the whole ListView or RecyclerView to get it’s content? So far, I am not sure it’s possible since I can get only elements visible on the screen. Do you have any thoughts or workarounds on how to get content of ListView not displayed on screen ?

haha :smiley:, I think I already have the answer from you: To get all the elements from the Scrollable list

@olyv yes. Only scroll.

this of my code is scrolling down but how can i make it to scroll up also?

public static boolean scrollToElement (By by) throws Exception {
boolean isFoundTheElement = driver.findElements(by).size() > 0;
while (isFoundTheElement == false) {
swipeVertical(0.8, 0.1, 0.5, 2000);
isFoundTheElement = driver.findElements(by).size() > 0;
}

		  return isFoundTheElement;
		}

		public static void swipeVertical (
		  double startPercentage, double finalPercentage, double anchorPercentage, int duration)
		  throws Exception {
		  org.openqa.selenium.Dimension size = driver.manage().window().getSize();
		  int anchor = (int) (size.width * anchorPercentage);
		  int startPoint = (int) (size.height * startPercentage);
		  int endPoint = (int) (size.height * finalPercentage);
		  getTouchAction().press(PointOption.point(anchor, startPoint))
		  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
		  .moveTo(PointOption.point(anchor, endPoint)).release().perform();
		}

		public static TouchAction getTouchAction () {
		  return new TouchAction(driver);
		}

}

The following code works fine for me.

public WebElement scrollToAnElementByText(AppiumDriver<WebElement> driver, String text) {
        return driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector())" +
                ".scrollIntoView(new UiSelector().text(\"" + text + "\"));"));
}
4 Likes

Thank you Anto_Rodrigo

This got worked for me.

I am searching for year from drop down, and it is present at the end of that drop down but its not visible whenever I open that drop down. So I used function that you have mentioned above and it got worked for me very well.

1 Like

Sounds good :slight_smile: @Mayuresh92

do you know how to scroll horizontal? if yes please let me know codes.

Hi @Aleksei
i am facing java.lang.NullPointerException :

This is my code
public WebElement scrollToAnElementByText(AndroidDriver driver1, String text)
{
return Adriver.findElement(MobileBy.AndroidUIAutomator
(“new UiScrollable(new UiSelector())” +""
+ “.scrollIntoView(new UiSelector().text(”" + text + “”));"));
}

JavascriptExecutor js = (JavascriptExecutor) Adriver;

// HashMap<String, String> scrollObject = new HashMap<String, String>();

// scrollObject.put(“direction”, “down”);

// js.executeScript(“mobile: scroll”, scrollObject);

Can you please help me to resolve the issue!

public WebElement scrollToAnElementByText(AndroidDriver driver1, String text)
{
return Adriver.findElement(MobileBy.AndroidUIAutomator

see driver1 vs Adriver

19%20AM|226x499

in the test page
Pray4MeRequestPage p1 = new Pray4MeRequestPage(driver1);
p1.scrollToAnElementByText(driver1, “Submit”);

in the find element page, in this page i have declared & initialised Android driver as Adriver
public WebElement scrollToAnElementByText(AndroidDriver Adriver, String text)
{
return Adriver.findElement(MobileBy.AndroidUIAutomator
(“new UiScrollable(new UiSelector())” +""
+ “.scrollIntoView(new UiSelector().text(”" + text + “”));"));
}
i am using POM frame work,
there is resource id, i have only text to find element, initially when the page opens Submit button will be hidden i need to scroll to submit button.
Can you please help me

Thanks, it works for me. Tried scroll down with native app, android

Thank for your sharing

I share my code for who needs

public static void scrollToText(AndroidDriver<MobileElement> driver, String text) {
	MobileElement el = (MobileElement) driver.findElementByAndroidUIAutomator("new UiScrollable("
			+ "new UiSelector().scrollable(true)).scrollIntoView(" + "new UiSelector().text(\"" + text + "\"));");
}

public static void scrollToId(AndroidDriver<MobileElement> driver, String id) {
	MobileElement el = (MobileElement) driver.findElementByAndroidUIAutomator(
			"new UiScrollable(" + "new UiSelector().scrollable(true)).scrollIntoView("
					+ "new UiSelector().resourceIdMatches(\"" + id + "\"));");
}```
2 Likes

Hello Aleksei
I tried with this code but it’s throwing error

driver.findElement(MobileBy.AndroidUIAutomator(“new UiSelector().resourceIdMatches(".*id / com.barclays.absa.banking.uat:id/floatingActionButton"]")”));

→ replace to:

driver.findElement(MobileBy.AndroidUIAutomator(“new UiSelector().resourceIdMatches(”.*id/floatingActionButton"]")"));