This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
22 / 24
Sep 2018

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!!!

4 months later

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!

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.name28(elementName1));
WebElement abc2 = driver.findElement(By.name28(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’.

11 days later

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

2 months later

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

2 months later

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);

20 days later

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

2 months later

Hi,
When I use the below statement, it always search from the top of the screen to search for the matching text.

UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + selector + “”).instance(0))"));

Consider a scenario where there two elements Element1 and Element2 displaying invisible before scrolling and Element1 is having matching text of ‘permission’ and Element2 is having matching text of ‘Submit’.

When I use the command for Element1 as below, it searches from the top of the screen for the text ‘permission’ and if finds the match, it stops there.
UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + “permission”+ “”).instance(0))"));

And now, when I use the command for Element2 as with matching text ‘Submit’, it again scrolls to the top to find the matching for the text ‘Submit’.
UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + “Submit”+ “”).instance(0))"));

When Element1 is already found, shouldn’t the search for ‘Element2’ start from the position after ‘Element1’?
If the command using textMatches is made to work like always searching from the top, is there any alternative best approach to scroll to any particular element?

I already tried the below method also but did not find it effective way:

org.openqa.selenium.Dimension size1 = driver.manage().window().getSize();
int x1 = size1.getWidth() / 2;
int starty1 = (int) (size1.getHeight() * 0.70);
int endy1 = (int) (size1.getHeight() * 0.10);
driver.swipe(x1, starty1, x1, endy1, 3000);

Please let me know your responses.

5 months later

Hi Every one is any one having knowledge over react native application automation , if yes then please help me

11 days later

I am using following method which scroll until element present and then return element. Hope this will help.
public AndroidElement ScrollToElement(String by, String using, int miliseconds) {
AndroidElement element = null;
int numberOfTimes = 10;
Dimension size = driver.manage().window().getSize();
int anchor = (int)(size.width / 2);
//Swipe up to scroll down
int startPoint = (int)(size.height - 10);
int endPoint = 10;

for (int i = 0; i < numberOfTimes; i++) {
try {
new TouchAction(driver)
.longPress(point(anchor, startPoint)) //.press(point(anchor, startPoint)) if used press need proper waiting time
//.waitAction(waitOptions(ofMillis(miliseconds)))
.moveTo(point(anchor, endPoint)).release().perform();
element = (AndroidElement) driver.findElement(by, using);
i = numberOfTimes;
} catch (NoSuchElementException ex) {
System.out.println(String.format(“Element not available. Scrolling (%s) times…”, i + 1));
}
}
return element;
}

In your test use as follows:
Example:
String usingWebView = “//android.widget.TextView[@text=‘Spinner’]”;
String by = “xpath”;
MobileActions mbAct = new MobileActions(driver); //This is just a class which has above function code.
AndroidElement webViewElement = mbAct.ScrollToElement(by, usingWebView, 250);
webViewElement.click();

19 days later

This of my code is scrolling down but how can i make it to scroll up also?

public static boolean scrollToElement (By by) throws Exception {
boolean isFoundTheElement = driver.findElements(by).size() > 0;
while (isFoundTheElement == false) {
swipeVertical(0.8, 0.1, 0.5, 2000);
isFoundTheElement = driver.findElements(by).size() > 0;
}

		  return isFoundTheElement;
		}

		public static void swipeVertical (
		  double startPercentage, double finalPercentage, double anchorPercentage, int duration)
		  throws Exception {
		  org.openqa.selenium.Dimension size = driver.manage().window().getSize();
		  int anchor = (int) (size.width * anchorPercentage);
		  int startPoint = (int) (size.height * startPercentage);
		  int endPoint = (int) (size.height * finalPercentage);
		  getTouchAction().press(PointOption.point(anchor, startPoint))
		  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
		  .moveTo(PointOption.point(anchor, endPoint)).release().perform();
		}

		public static TouchAction getTouchAction () {
		  return new TouchAction(driver);
		}

}