How the scrollTo() and scrollToExact() methods are working?

I’m trying to figure out how the two methods are working, since it doesn’t scroll the screen.

I’m using the scrolling method like that:

driver.scrollTo("Register"); //where "Register" is the text displayed on the button

Should it work like that? What if I want to scroll to an element and find it By its Id?

4 Likes

See if my explanation here helps: https://github.com/appium/appium/issues/3974

1 Like

How do we use this with python client??

I am currently blocked with this :cold_sweat:

2 Likes

@rOKi125, @0x1mason @mariusbob
IF you are looking for solution how driver.scrollTo works you can watch my video, I’m using the latest version of applium jar files

1 Like

@Appium_Master: could you please post here a code snippet? I just want to see what “generateY” does.
Thanks!

@mariusbob :-
public double generateX(Point point)
{
int x=point.getX();
return x;

}
public static int generateY(Point point)
{
    int y=point.getY();
    return y;

}

Hi @Appium_Master,

Could you please help me on working with scrollTo for a webview? As using scrollTo on webview moves past the desired element to end of page.

Is there any workaround for working on webview?

Thanks in advance,
Sujata

1 Like

@sujata_kalluri :- Sure I will help on this.
Please give me your code to identify the issue and also let me know what appium version, Client lib are you using ?

Hi @Appium_Master ,

Appium version: 1.3.4.1

Code:
((AndroidDriver)driver).context(“NATIVE_APP”);
Thread.sleep(1000);
((AndroidDriver)driver).scrollTo(val).click(); //the control moves past “val”, after scrolling to desired element
((AndroidDriver)driver).findElementByXPath("//android.widget.GridView/android.view.View["+rowno+"]//android.widget.Image[1]").click();
Thread.sleep(2000);
((AndroidDriver)driver).context(“WEBVIEW”);
Thread.sleep(1000);

@sujata_kalluri :- Hi Sujata,
First of all sorry for the late reply, As per my knowledge driver.scrollTo(value) only working when android.widget.ListView have single text in it and it fails when multiple text is present in each row of listView. In my case I have single entry/text in each line.
So to resolve your issue you can use below method which I implemented for me and it is working fine.

public WebElement scroll(String keyword)
{
HashMap<String, String> scrollObject = new HashMap<String, String>();
RemoteWebElement element = (RemoteWebElement) driver.findElementByAndroidUIAutomator(
“new UiSelector().className(“android.widget.ListView”)”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String webElementId = ((RemoteWebElement) element).getId();
System.out.println(webElementId);System.out.println(element);
scrollObject.put(“text”, keyword);
scrollObject.put(“element”, webElementId);
js.executeScript(“mobile: scrollTo”, scrollObject);
return element;
}

Thank you

Regards,
Sujata

Our app doesn’t use android.widget.ListView, all elements are loaded in android.view.View, I can see all the elements in the uiautomatorviewer element tree. But I have to swipe screen to access the elements which aren’t in visible range. I tried uiautomator scrollintoview method, it does’t work for this case.

Hi @Guanhua_Jing,

I have used below code for scroll in my application. When a click to element does not happen, when element is not visible,an exception occurs using this I added this code. Hope this helps you.

js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollTo(0,0)”);
do
{
try
{
driver.findElement(By.xpath().click();
break;
}
catch(Exception e)
{
js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollBy(0, 200)”);

}
} while(true);

Thanks,
Sujata

Hi Appium_Master

I have tried your code for scrolling in the android app. below is the code, But still not able to scroll :-

	HashMap<String, String> scrollObject = new HashMap<String, String>();
	RemoteWebElement element = (RemoteWebElement)((AndroidDriver) driver).findElementByAndroidUIAutomator("new UiSelector().className(\"android.view.View\")");
	JavascriptExecutor js = (JavascriptExecutor) driver;
	String webElementId = ((RemoteWebElement) element).getId();
	System.out.println(webElementId);System.out.println(element);
	scrollObject.put("text", "Load more");
	scrollObject.put("element", webElementId);
	js.executeScript("mobile: scrollTo", scrollObject);
	element.click();

But i am getting the below exception

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.

at line

js.executeScript(“mobile: scrollTo”, scrollObject);

Could you please help me in how to do scrolling in android app.

I am using below versions:-

Appium 1.3.4.1
Selenium 2.45
Java-Client 2.2.0

Thanks in Advance!!

I’ve found scrolling by co-ordinates helps. In Android you scroll from bottom up to get the scroll working. The best way i’ve found to work is - either to get the location of an element :
int topY = element.getLocation().getY()
int bottomY = topY + element.getSize().getHeight()
int centerX = element.getLocation().getX() + (element.getSize().getWidth()/2)
driver.swipe(centerX, bottomY, centerX, topY, duration);
Or - get the size of the screen, divide it up and scroll like that -
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeighStart.toInt();
Double screenHeightEnd = dmensions.getHeight() * 0.2
int scrollEnd = screenHeightEnd.toInt();
driver.swipe(0,scrollStart,0,scrollEnd,duration); //duration is in milliseconds I believe so go for values like 1000 or 2000

The reason you need to convert the Doubles to ints is because driver.swipe does not accept Double/double values.

I hope this helps.

3 Likes

@Rjimms, Thanks your solution worked well for Android scroll action. I have tried same code for iOS also, but it doesn’t worked for me. If you know solution for iOS also, please provide. I am searching for reliable solution for iOS scroll and swipe.

I don’t have any experience with iOS unfortunately.

1 Like

@Rjimms, K Thanks…

Hi Selvi,

Can you try scroll on iOS app with below strategy…

driver = new IOSDriver(new URL(“http://127.0.0.1:4725/wd/hub”), capabilities);
WebDriverWait wait = new WebDriverWait(driver,150);

  driver.findElementByAccessibilityId("UICatalog").click();
  TouchAction act = new TouchAction(driver);
 act.press(driver.findElementByAccessibilityId("Sliders")).moveTo(driver.findElementByAccessibilityId("Alert Controller")).release().perform();
  Thread.sleep(4000);

// Just make sure ur locator is correct, area should be scrollable and it should work…
If u have some text in content-desc attribute of Node then above code will work…
Try with className/xpath

Will this works(moves) , if the element is not even displaying on screen and it exists some where else ?

Please let me if this is possible !!!

Thanks,
Bhaskar.