Appium 1.6.3, How can I scroll to particular element in java

I am using appium 1.6.3, and IOSDriver, what are the ways available to scroll to the particular element on page

Environment

Appium version : 1.6.3
Desktop OS/version used to run Appium: 10.12 (16A323)
npm version : 3.10.9
Mobile platform/version under test: 10.0.2(14A456)
Real device or emulator/simulator: Real device

Use scrollTo or scrollToexact method.

those got deprecated

this is what we did in c# as a separate method to be called in any test suite

//Sleep needed in the cases of navigating away from a screen and coming back to. This gives the driver a chance to catch up.
Thread.Sleep(700);
try
{
//UiSelector arguments must only call “scrollIntoView” method or methods on UIScrollable whcih return UiScrollable or UiObject objects
return driver.FindElement(MobileBy.AndroidUIAutomator(“new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(”" + selector + “”).instance(0))"));
}
catch
{
//Added because sometimes Android doesn’t scroll all the way
IWebElement frame = driver.FindElements(By.ClassName(“android.widget.ListView”)).ElementAt(0);
int startx = frame.Location.X + (frame.Size.Width / 2);
int starty = frame.Location.Y + ((frame.Size.Height / 2) - 5);
int endx = startx;
int endy = starty -100;
driver.Swipe(startx, starty, endx, endy, 400);

                return driver.FindElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + selector + "\").instance(0))"));

@kcinman11358
below code works for me
((AppiumDriver) driver).scrollToExact(“Text”);

@er.pradeep4 what version of appium are you using?

I am using IOSDriver and appium version 1.6.3 with java - scrollTo or scrollToExact(“Text”); are no more supported.

Solution What I found is -

WebElement abc = driver.findElement(MobileBy.AccessibilityId(locator));

int x = abc.getLocation().getX();
int y = abc.getLocation().getY();

TouchAction action = new TouchAction(driver);
action.press(x,y).moveTo(x+90,y).release().perform();

in case any body need,

It works well, Thank You!!!

1 Like

Hi!

I tried implementing your solution but it just scrolls up, and not down as I expect. I also tried increasing the pixels, and changing the plus to minus as i assumed it will change the direction but it did not. Could you please help me with this ?

Thanks!
S.

Hey!

Nevermind! I used driver.swipe() to get what I wanted. Thank you!

1 Like

Hi Schumin,

May u share with me your solution? Currently i can not work with “Scroll to Element”. Thank you.

Hey!

This is what I created to use throughout my test suite.

public static void scrollToElement(IOSDriver driver,String elementName1, String elementName2) {
WebElement abc = driver.findElement(By.name(elementName1));
WebElement abc2 = driver.findElement(By.name(elementName2));
int x = abc.getLocation().getX();
int y = abc.getLocation().getY();
int x1 = abc2.getLocation().getX();
int y1 = abc2.getLocation().getY();
driver.swipe(x1, y1, x, y, 1);
}

Basically, Element 1 is an element that is visible on the screen somewhere at the top and Element 2 is visible on the screen somewhere towards the bottom.

Hope this helps!

Cheers!
S

Try the below method, one which works from version 3.0.0 to the latest 4.1.2
Ex: java-client 4.1.2

Method:
String selector = “searchString”;
driver.findElement(MobileBy.AndroidUIAutomator(“new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().text(”"+selector+""));"));

The method will scroll in the page until it finds the string with ‘searchString’.

1 Like

hi can any one help me out that i have to scroll the page i used swipe method

dr.swipe(0, 20,20, 40, 1000);

but it is not working

i used

((AppiumDriver) driver).scrollToExact(“Text”); this one also but not working

Rini, have you tried the above method.
If not try out and let me know.
If yes, lets share the logs.

((AppiumDriver) driver).scrollToExact(“Text”);

i am using java language

hi @arun_mangan

FAILED: testregistration
java.lang.NullPointerException
at defaultproject.wataap.testregistration(wataap.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0

scrollTo() and scrollToExact() are not consistent thats why its deprecated in 1.7.0
so use swipe() method

On iOS, you can use the code below:

JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put(“direction”, “up”);
scrollObject.put(“xpath”, “//XCUIElementTypeStaticText[@name=“NAME”]”);
js.executeScript(“mobile: swipe”, scrollObject);

1 Like

Hi, can you please give a code-snippet on how to scroll using swipe method? Is it based on location or based on objects?

Found a solution at least for Android -

Use the below code snippet.

public void scrollDown() {
	
	    int pressX = driver.manage().window().getSize().width / 2;
	 
	    int bottomY = driver.manage().window().getSize().height * 4/5;
	 
	    int topY = driver.manage().window().getSize().height / 8;
	    
	    int i = 0;
	    
	    do{
	    	    	
	    	isPresent = driver.findElements(By.id("urid")).size()>0;
	      	if(isPresent){
	    		 we = driver.findElement(By.id("urid"))
	    		we.click();
	    		break;
	    	}
	      	else{
	    	scroll(pressX, bottomY, pressX, topY);}
	    i++;
	    
	    } while(i <= 4);
	}

	private void scroll(int fromX, int fromY, int toX, int toY) {
	    TouchAction touchAction = new TouchAction(driver);
	    try{
	    catch(WebDriverException wd){
	    	 }
	    touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();

	}

This can be improved, this is just a raw material. :slight_smile: Let me know if it doesn’t work for Android.

Appium version - 1.7.2
Java Client - 5.0.2 - BETA