Issue with scrollTo and scrollToExact methods of Android Driver. Methods do not scroll to required element text, but scroll to end of page

I think better to develop an algorithm. pseudo could be like this:

Take the list of visible elements
take position of first visible element
take position of last visible element
swipe from last visible element to first visible element
continue swiping until the list of all visible elements is not same

Hope it would work.

driver.scrollTo(“some text”) crashes :’( … The app stops there and the automation stops … Need urgent help

This worked for me on Android with Appium Python client:

element_to_tap = self.driver.find_element_by_id(“com.demoapp.cool:id/mySizeButton”)
element_to_drag_to = self.driver.find_element_by_id(“com.demoapp.cool:id/guideToSizeButton”)
self.driver.scroll(element_to_tap, element_to_drag_to)
sleep(10)

get the id from your android app and replace it in the places of ‘mySizeButton’ and ‘guideToSizeButton’

Hi,
Can you please elaborate your answer?
element_to_tap and element_to_drag_to means exactly what you trying to do?

In simple words, consider you have a list of options inside the menu page - A, B, C, D & E while E is at the end and you have to scroll up to view it. Assume with a real device in hand you will manually try to scroll up, so you will put finger in point 2 and scroll it till point 1 (down to up finger movement). I’m doing the same in my code.

A
B
C
D

E
element_to_tap = D
element_to_drag_to = B

and say Appium to scroll up from D to B, so my screen will move up and I will be shown the options in the bottom. Hope this helps you understand it better.

@Venkatesh_Akula : Thanks. This helped me out.

i get NoMethodError:
undefined method `scroll’ for #Selenium::WebDriver::Driver:0x007fe9b33bda40

trying to use @driver.scroll(@driver.find_element(:name, “ZIP CODE”), @driver.find_element(:name, “LEGAL LAST NAME”))

Hello,

May you help me please? explain me what is with that “self.driver”. I try to learn how to use java and Appium and my Eclipse does not recognize that “self”. Can you give me an concrete example how to use scroll, I need that method so much… please help me

Thank you!

Hi @mota,
I am also facing the same issue.how did you solve this issue.could you please post me the piece of code here?
I tried this
driver.scrollToExact(“Text”);
and
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int starty = (int) (size.height * 0.60);
int endy = (int) (size.height * 0.10);
driver.swipe(x, starty, x, starty, 2000);
}
also I tried @sujata_kalluri solution too.But this gives me exception.
Please help me out if you got the solution.

Hey, I haven’t worked with Java sorry.

HI MItesh,

I have a screen with dynamic elements and i would like to click on each one of them. So i have created the following code. The scroll goes up and down and it clicks on most of the elements, however it doesn’t click on all of them (when test finishes all is successful and no error messages). Any work around this? thank you in advance

@When("^I click on the available excursions$")
public void i_click_on_the_available_excursions() throws Throwable {
wait.until(ExpectedConditions.visibilityOf(destinationDetailsScreen.Back_Btn));
driver.scrollTo(“destinationCTA”);

   List<MobileElement> linkSearch = driver.findElements(By.xpath("//android.widget.Button[@content-desc='destinationCTA.']"));

    for (int i=1; i<=linkSearch.size(); i++)
    {
        linkSearch = driver.findElements(By.xpath("//android.widget.Button[@content-desc='destinationCTA.']"));
        wait.until(ExpectedConditions.visibilityOf(linkSearch.get(i-1)));
        linkSearch.get(i-1).click();
        destinationDetailsScreen.Back_Btn.click();
    }

In recent update appium “mobile : scroll” deprecated, following code will work and video will help you to implement.

Scroll to text :

MobileElement radioGroup = (MobileElement) wd

.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"

".resourceId(\"+<listview_id>+\")).scrollIntoView("

"new UiSelector().text(\"+<your_text>+\"));");

radioGroup.click();

This link will help you : https://www.youtube.com/watch?v=bT3tqaLNn-Y

hello,
i am using Appium v1.6.3 and java client 4.1.2. i am able to scroll on page top to bottom. scrollTo function is not supported .please help and share the code snippet how to scroll down and click on element.
i used touchaction class but it’s not working.
TouchAction action = new TouchAction(driver);
// action.longPress(540,1725).moveTo(577,497).release().perform();

You can use swipe command and check after every swipe if element is visible

Thanks Mitesh.
swipe is working but i am not able to scroll to end of page. also not able to find element.
public void swipe_vertical(){

	Dimension size = driver.manage().window().getSize();
	//int height = size.height;
	//int width = size.width;
	System.out.println("height=" +size.height+"width="+size.width);
	int startx = size.width/2;
	int endx = size.width/2;
	int starty = size.height - size.height/4;
	int endy = size.height/4;
	System.out.println("vertical swipe");
	driver.swipe(startx, starty, endx, endy, 600);

You can create a webelement and get location of that element and use it in
swipe command​. You can refer below code for getting coordinates location:​

Web​Element element = driver.findElement​By().getLocation();
int e1 = element.getX();

Hello Mitesh,
i am using this code but it’s not working.Please write code for me. i am not able to scroll till end of page and also not able to click on element.

public void swipe_vertical(){

	Dimension size = driver.manage().window().getSize();
	//int height = size.height;
	//int width = size.width;
	System.out.println("height=" +size.height+"width="+size.width);
	int startx = size.width/2;
	int endx = size.width/2;
	int starty = size.height - size.height/4;
	int endy = size.height/4;
	System.out.println("vertical swipe");
WebElement element = driver.findElementByAndroidUIAutomator("Uiselector().className(\"android.widget.LinearLayout[[5]\").text(\"Snapdeal\")");
	
	do{
		driver.swipe(startx, starty, endx, endy, 600);
	}while(!element.isDisplayed());

Hello shilpi,

try this…

//WebElement element is the one till you want to scroll
public void swipeTo(WebElement element) {
int startx = 0, starty = 0, endx = 0, endy = 0;
Dimension size = driver.manage().window().getSize();
try {
TouchAction tAction = new TouchAction(driver);

        startx = (int) (size.width * 0.5);
        endx = (int) (size.width * 0.5);
        starty = (int) (size.height * 0.6);
        endy = (int) (size.height * 0.4);

        while (!element.isDisplayed()) {

            //First tap on the screen and swipe it right using moveTo function
            tAction.press(startx + 20, starty + 20).moveTo(endx + 20, endy + 20).release().perform();


            Thread.sleep(400);
            //Second tap on the screen and swipe it left using moveTo function
            tAction.press(endx + 20, endy + 20).moveTo(startx + 20, starty + 20).release().perform();

            Thread.sleep(400);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

hope this information will help you :slight_smile:

Thanks Sachin,
Actually i am scrolling top to bottom. it’s working but i am not able to click on element in a list .

public void swipe_vertical() throws InterruptedException{

	Dimension size = driver.manage().window().getSize();
	int height = size.height;
	int width = size.width;
	System.out.println("height=" +size.height+"width="+size.width);
	int startx = size.width/2;
	int endx = size.width/2;
	int starty = size.height - size.height/4;
	//int starty = size.height - size.height/6;
	//int starty = size.height - size.height/8;
	int endy = size.height/4;
	System.out.println("vertical swipe");
	
	driver.swipe(startx,starty,endx,endy,200); 
	Thread.sleep(10000);

and

	///List <WebElement> element = driver.findElementsByClassName("android.widget.LinearLayout");
	//for (int i=1;i<=element.size();i++){
		//if(!element.get(i).isDisplayed()){
		//driver.swipe(startx, starty, endx, endy, 200);	
		//}

using this i am not able to click on a element.

Hello Shilpi,

Probably you are missing to give the click event. Please try this below code:
try{
while(!driver.findElement.isDisplayed()){
driver.swipe(startx, starty, endx, endy, 200);
}
}catch(Exception e){
driver.swipe(startx, starty, endx, endy, 200);
}

driver.findElement.click();

Also handle the exception for this code.