How to scroll down to click the element in Android using appium and java?

41,270

Solution 1

I tried this solution and it is worked for me.

public void scrollAndClick(String visibleText) {
     androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
        }
    }

Solution 2

In new versions of Appium you can use this:

TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
element.click();

Solution 3

Please use the code below. It will scroll till the text is visible.

   String uiSelector = "new UiSelector().textMatches(\"" + text
                        + "\")";

   String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
                        + uiSelector + ");";

    driver.findElementByAndroidUIAutomator(command);

Now you could perform the click action after this.

Solution 4

public AndroidElement ScrollToElement(String by, String using) {
    AndroidElement element = null;
    int numberOfTimes = 10;
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width / 2);
    // Swipe up to scroll down
    int startPoint = (int) (size.height - 10);
    int endPoint = 10;

    for (int i = 0; i < numberOfTimes; i++) {
        try {
            new TouchAction(driver)
                .longPress(point(anchor, startPoint))
                .moveTo(point(anchor, endPoint))
                .release()
                .perform();
            element = (AndroidElement) driver.findElement(by, using);
            i = numberOfTimes;
        } catch (NoSuchElementException ex) {
            System.out.println(String.format("Element not available. Scrolling (%s) times...", i + 1));
        }
    }
    return element;
}

In your test use as follows: Example: String usingWebView = “//android.widget.TextView[@text=‘Spinner’]”; String by = “xpath”; MobileActions mbAct = new MobileActions(driver); //This is just a class which has above function code. AndroidElement webViewElement = mbAct.ScrollToElement(by, usingWebView, 250); webViewElement.click();

Share:
41,270
Osanda Deshan
Author by

Osanda Deshan

A highly focused Associate Technical Specialist experienced in a variety of automation and engineering positions. Organized, methodical and a keen eye for detail results in solid coding and trustworthy automation scripting. Understanding client requirements and communicating the progress of projects are core values in achieving long lasting business relationships.

Updated on February 09, 2022

Comments

  • Osanda Deshan
    Osanda Deshan over 2 years

    I would like to know how to scroll down to click the element in Android using appium and java?

    I am having a list of elements inside "android.support.v7.widget.RecyclerView". Since it has more than 10 elements, we need to swipe the screen to see the below elements. Each element has same id which is "com.osanda.exampleapp/textViewTitle". But their texts are different like "Apple", "Orange", "Grapes"......

    All I need is to scroll and click the relevant element using its text("Apple", "Orange", "Grapes".....)

    I have followed many tutorials but couldn't do it properly. I have managed to scroll down the screen. But it won't work when the element is in the middle position of the scroll.

    When I listed the element names it only shows the visible elements, not all elements.

    List<WebElement> elements = androidDriver.findElementByClassName("android.support.v7.widget.RecyclerView").findElements(By.id("com.osanda.exampleapp:id/textViewTitle"));
            for(WebElement element : elements) {
                System.out.println(element.getText());
            }
    

    Thank You.