How to scroll to a specific Element

Is there any method to scroll to specific element to work with Android application?

In Appium we had “scroll” and “scrollTo” methods which were deprecated. But with use of UISelector we still use scroll and click on any specific element.

Is there a way to do the same in AppiumStudio?

Is Swipe is the only option? Swipe may not work all times, reason being we dont know how many swipes are required to swipe till that element.

So please help us how to achieve scroll to element.

1 Like

Hi @udayanem,

As I have experienced SeeTestDriver does not have scrollTo or scroll methods. However as an alternate way swipe will work fine.

As you have stated your problem, here is a solution for that:

Create a function to checkElementVisibility:

public boolean checkElementVisibility( String xpath) {
try {
driver.findElement(By.xpath(xpath));
return true;
}
catch (Exception e) {
return false;
}
}

Then try swiping until the element has been found [or checkElementVisibility(“Your_xpath”) == true]

Hi @AshrafRizvi01,

I had the same thought and implemented below method.

	//This method scrolls downwards / swipe upwards only
	public void scroll(AppiumDriver driver,By by)
	{
		boolean found=false;
		while(!found)
		{
			try
			{
				driver.findElement(by);
				found=true;
			}
			catch(Exception e)
			{
				Dimension size = driver.manage().window().getSize();
				int startx=size.width/2;
				int starty=(int)(size.height*0.9);
				int endx=size.width/2;
				int endy=(int)(size.height*0.2);
				driver.swipe(startx, starty, startx, endy, 1000);
			}
		}
	}

We can call the method something like below, however we can implement this method with By or text etc…
By weBy=By.xpath(“xpath=//*[@text=‘foo’]”);
scroll(driver,weBy);

Is there a way that we can find out we scrolled end of the search list or something? Because the above logic works out as far as the search string/object exists. Incase if the object doesnt exists, and if we are at end of the list then we should throw that item not found.

Is there any better way handling the above?

2 Likes

It will take a little time but here is a suggestion:
Check for the last element (Bottom) on the screen in the catch block and every time compare it with previous one. If both are same you got the bottom of the page.

Or if it’s a list of some kind or a view group, try this: Check for the last group just below your scrollable items. There will be a viewgroup to provide easy scroll. Find the childs of that viewgroup

try {
SeeTestElement parent = driver.findElement(By.Id(“Viewgroup_id”);
List childs = parent.findElements(By.ClassName(“Element Class Name”);
if(childs.size( ) == 0) {
//bottom of the page
}
else {
//scroll
}
}
catch(Exception e) {
//bottom of the page
}

To bad swipe has depreciated as well. :disappointed: