Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 240
Osanda Deshan Avatar asked Oct 03 '17 17:10

Osanda Deshan


People also ask

How do I scroll down to a specific element in appium?

Appium comes up with a rich class UiScrollable, which makes it possible to scroll down to the page and perform actions on elements. It is a powerful Android class that performs element lookups in scrollable layouts. scrollIntoView class performs scroll action until the destination element is found on the screen.

How do you scroll down until end in appium?

scrollTo(); and driver. ScrollToExact();

How do you scroll into view in appium?

In most cases you should use "scrollIntoView" class which performs scroll action until the destination element is found on the screen.


1 Answers

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();
        }
    }
like image 84
Osanda Deshan Avatar answered Nov 14 '22 21:11

Osanda Deshan