Scrolling to element in android app using java

I am trying to scroll to element which is not visible on screen

The following code i am using
WebElement abc = driver.findElement(By.id(“elemnt1”));

    int x = abc.getLocation().getX();
    int y = abc.getLocation().getY();

    TouchAction action = new TouchAction((MobileDriver) driver);
    action.press(x,y).moveTo(x+90,y).release().perform(); 

the sample code says
TouchAction action = new TouchAction(performsTouchActions); action.press(element).waitAction(300).moveTo(element1).release().perform();

// https://appium.github.io/java-client/io/appium/java_client/TouchAction.html

how can you get instance of performsTouchActions

can you please help me what is the error i am doing

@guru_lakshman_tomman try another approach

String scrollViewContainer_finder = "new UiSelector().resourceIdMatches(\".*id/your_scroll_view_id\")";
String neededElement_finder = "new UiSelector().resourceIdMatches(\".*id/elemnt1\")";

WebElement abc = driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(" + scrollViewContainer_finder + ")" +
                ".scrollIntoView(" + neededElement_finder + ")"));

3 Likes

I am getting this exception
java.lang.IllegalArgumentException: The class org.openqa.selenium.remote.RemoteWebDriver of the given context doesn’t implement io.appium.java_client.FindsByAndroidUIAutomator nor io.appium.java_client.FindsByFluentSelector. Sorry. It is impossible to find something.

for the code
WebElement abc = driver.findElement(MobileBy.AndroidUIAutomator(“new UiScrollable(” + scrollViewContainer_finder + “)” +
“.scrollIntoView(” + neededElement_finder + “)”));

How can we get instance of performsTouchActions
we can use for this method

TouchAction action = new TouchAction(performsTouchActions);

  1. ok. let’s make it easier. let\s say there is element on screen which you can see. are you able to find element with:
WebElement el = driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceIdMatches(\".*id/your_id\")"));
  1. what is your java-client version? hope 5+?
  2. if you still want to swipe manually and check elements:
// method 1
((AndroidDriver) driver).swipe(savingPoint.getX(), savingPoint.getY(), balancePoint.getX(), balancePoint.getY(), 500);

// method 2 (same as above)
new TouchAction((AndroidDriver) driver).press(startx, starty).waitAction(Duration.ofMillis(500)).moveTo(endx, endy).release().perform();

// method 3 (variations)
// instead of press(startx, starty) -> use press(your_el)
  1. to see where actually happens touch and swipe enable “Show touches” in “Developer options” menu of your phone

I got this error
java.lang.IllegalArgumentException: The class org.openqa.selenium.remote.RemoteWebDriver of the given context doesn’t implement io.appium.java_client.FindsByAndroidUIAutomator nor io.appium.java_client.FindsByFluentSelector. Sorry. It is impossible to find something.

for the code
WebElement el = driver.findElement(MobileBy.AndroidUIAutomator(“new UiSelector().resourceIdMatches(”.*id/title_vungle")"));

Thanks for your help let me explain what i really want

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

Also if you can provide full code/ class it will be great help

you have two options:

  1. first approach is use UiSelector which is not working with your config by some reason (you still did not write anything about your appium version and java-client version).
  2. second is your first approach when you scroll and check… scroll and check.

choice is yours.

Sorry for late reply please find my total code
My java version 1.8 ( I am using android studio jdk)
appium version 1.6.5 (appium -v)

i am trying to click using
driver.findElement(By.id(“com.appredeem.apptrailers:id/textview5”)).click();

but it is not in mobile screen it is only visible once i scroll to bottom
the code

public class AppTrailersTest {

WebDriver driver;
long minute_delay = 60000 ;
AppiumServerJava appiumServerJava;


@Before
public void setUp() throws MalformedURLException {
    appiumServerJava = new AppiumServerJava();
    // start appium appiumServer
    appiumServerJava.startServer();

    // Created object of DesiredCapabilities class.
    DesiredCapabilities capabilities = new DesiredCapabilities();

    // Set android deviceName desired capability. Set your device name.
    capabilities.setCapability("deviceName", "LC51GY642544");
    //  capabilities.setCapability("deviceName", "V20270160320032782");

    // Set BROWSER_NAME desired capability. It's Android in our case here.
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

    // Set android VERSION desired capability. Set your mobile device's OS version.
    capabilities.setCapability(CapabilityType.VERSION, "4.4.2");

    // Set android platformName desired capability. It's Android in our case here.
    capabilities.setCapability("platformName", "Android");
    // this willnot delet app data and app registration details
    capabilities.setCapability("noReset", "true");

    // set timeout to 5minutes
    capabilities.setCapability("newCommandTimeout", 300);

    // Set android appPackage desired capability. It is
    // com.android.calculator2 for calculator application.
    // Set your application's appPackage if you are using any other app.
    //  capabilities.setCapability("appPackage", "com.google.android.calculator");
    capabilities.setCapability("appPackage", "com.appredeem.apptrailers");

    // Set android appActivity desired capability. It is
    // com.android.calculator2.Calculator for calculator application.
    // Set your application's appPackage if you are using any other app.
    //    capabilities.setCapability("appActivity", "com.google.android.calculator.Calculator");
    capabilities.setCapability("appActivity", "com.appredeem.apptrailers.AppTrailers");


    // Created object of RemoteWebDriver will all set capabilities.
    // Set appium server address and port number in URL string.
    // It will launch calculator app in android device.
    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(150, TimeUnit.SECONDS);


}

@Test
public void testFirstCalculator() {
    driver.findElement(By.id("com.appredeem.apptrailers:id/textview5")).click();

    driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
}

@After
public void End() {
    // driver.manage().timeouts().wait(300,TimeUnit.SECONDS);
    // driver.quit();

    try {
        Thread.sleep(minute_delay * 5);
    }catch(Exception e) {
        e.printStackTrace();
    }
    driver.quit();

    appiumServerJava.stopServer();

}

}

Not java but Appium java-client version needed.

HI it is latest version java-client-5.0.0-BETA8

all looks good. “MobileBy.AndroidUIAutomator” should work.
is problematic ?

driver.findElement(By.id("com.appredeem.apptrailers:id/textview5")).click();
``

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

}