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

Working, but how you come across those numbers(coordinates)?
Is there any more sophisticated way of scrolling when dealing with webview?

@Bilal_Jarwan
I tried to use the below code…
Dimension size = driver.manage().window().getSize();
int starty=(int)(size.height0.5);
int endy=(int)(size.height
0.2);
int startx=size.width/2;
driver.swipe(startx,endy,startx,starty,2000);

But getting an error message like: “The method swipe(int, int, int, int, int) is undefined for the type AndroidDriver”. Can you please let me know the resolution

@Bilal_Jarwan
I tried to use the below code…
Dimension size = driver.manage().window().getSize();
int starty=(int)(size.height0.5);
int endy=(int)(size.height
0.2);
int startx=size.width/2;
driver.swipe(startx,endy,startx,starty,2000);

But getting an error message like: “The method swipe(int, int, int, int, int) is undefined for the type AndroidDriver”. Can you please let me know the resolution

Can you use the inspector tool to get your x/y coordinates?

Hey you can try this one ,works for me for an app devloped in react native.
I hope the comments will help you .
public void scrollUp() throws Exception {
// Get the size of screen.
Dimension size = driver.manage().window().getSize();
// Find swipe start and end point from screen’s with and height.
// Find start y point which is at bottom side of screen.
int starty = (int) (size.height * 0.80);
// Find end y point which is at top side of screen.
int endy = (int) (size.height * 0.20);
// Find horizontal point where you wants to swipe. It is in middle of
// screen width.
int startx = size.width / 2;

	// Swipe from Bottom to Top.
	driver.swipe(startx, starty, startx, endy, 3000);
	Thread.sleep(2000);
}

Sorry, but i have not tried for web view but you can use this single line code instead of defining a function and using it .i think there wont be an element without a text defining it, for what that element is .

6 months later

Hi, i have used the below code to Swipe / Scroll and it is working perfectly.
Code to Swipe UP
public boolean swipeFromUpToBottom()
{
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“direction”, “up”);
js.executeScript(“mobile: scroll”, scrollObject);
System.out.println(“Swipe up was Successfully done.”);
}
catch (Exception e)
{
System.out.println(“swipe up was not successfull”);
}
return false;
}
Code to Swipe DOWN
public boolean swipeFromBottomToUp()
{
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“direction”, “down”);
js.executeScript(“mobile: scroll”, scrollObject);
System.out.println(“Swipe down was Successfully done”);
}
catch (Exception e)
{
System.out.println(“swipe down was not successfull”);
}
return false;
}
Code for carousel images swipe

public boolean swipeImages()
{
try {
WebElement pageIndicator = driver.findElement(page_indicator);
String pageString= pageIndicator.getAttribute(“value”);
int length = pageString.length();
String count_string= pageString.substring(length-2, length).trim();
int count = Integer.parseInt(count_string);
System.out.println("Number of Image available to Swipe: "+count);
for (int i=0; i<=count; i++){
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“direction”, “right”);
js.executeScript(“mobile: scroll”, scrollObject);
}
System.out.println(“Swipe Successfully”);
}
catch (Exception e)
{
System.out.println(“Image swipe was not successfull”);
}
return false;
}

23 days later
public void swiptToBottom()
	{
		Dimension dim = driver.manage().window().getSize();
		int height = dim.getHeight();
		int width = dim.getWidth();
		int x = width/2;
		int top_y = (int)(height*0.80);
		int bottom_y = (int)(height*0.20);
		System.out.println("coordinates :" + x + "  "+ top_y + " "+ bottom_y);
		TouchAction ts = new TouchAction(driver);
		ts.press(x, top_y).moveTo(x, bottom_y).release().perform();
	}

Call the above method to swipe to bottom

10 days later

To do this you must know resource id or cont-desc of scrollable element. You also need to know className of your scrollable element.

If you have cont-desc in scrollable list

try {
    String scrollableList="your con-desc of scrollable List";
    String elementClassName="android.something.something";
    String anyText="any text";

    driver.findElement(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector().description(\"" + scrollableList + "\")).getChildByText("
                            + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
    }catch (Exception e){
            System.out.println("Cannot scroll further");
}

If you have resource-id in scrollable list

try {
    String scrollableList="your con-desc of scrollable List";
    String elementClassName="android.something.something";
    String anyText="any text";

    driver.findElement(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByText("
                            + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
 }catch (Exception e){
            System.out.println("Cannot scroll further");
}

If the screen cannot be scroll further it will throw error which will be catched by catch block.

19 days later

Hello guyz,

I was also stuck in scroll page issue today in my appium automation. i am using robot framework with appium library and running my automation real device using adb service.

i first tried using the appiumlibrary keywords Scroll · Scroll Down · Scroll Up but was getting error as

" WebDriverException: Message: Unknown mobile command “scroll”. Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported. "

then i tried implementing custom keywords using scroll methods via java & python code also but got the same error as above.

i even tried to use scroll up/down keywords of Androidlibrary of robot framework but it did’nt worked due import error as below:

Importing test library ‘AndroidLibrary’ failed: ImportError: cannot import name GLOBAL_VARIABLES

then finally i came up with 2 ways to scroll element as below:

  1. def scroll_page_to_text(self,text):
    driver = self.get_appium_webdriver_instance()
    driver.implicitly_wait(5000)
    element = driver.find_element_by_android_uiautomator(‘new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className(“android.widget.TextView”), "’
    + text + ‘")’)

    this i found on stackoverflow and worked for me

  2. if for some reasons the above did’nt work then use this alternative way of drag drop element. this is not good way but yes you’ll not be blocked atleast till you find a better way.

        def scroll_page_down(self,source_element_locator,destn_element_locator):
          	driver = self.get_appium_webdriver_instance()
             driver.implicitly_wait(5000)
            source_element=driver.find_element_by_xpath(source_element_locator)
            destn_element=driver.find_element_by_class_name(destn_element_locator)
            driver.drag_and_drop(source_element,destn_element)
    

this will work even if yours elements are not actually draggable/droppable. just make sure to pass locator of lowermost element of current screen as source_element_locator, then any element locator which is on top of screen as destn_element_locator. driver.drag_and_drop method will try to drag source_element to destn_element and it will automatically scroll your screen.

1 month 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);
	}

@Sarah_Paul
You just need to change the starting point of Y coordinates
here’s a snippet from my code
case “up”:
try {
new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
.waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, endVerticalY)).release()
.perform();

		} catch (Exception e) {
			log.info(swipeError + e);
		}
		break;
	case "down":
		try {
				new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
						.waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, startVerticalY))
						.release().perform();
			
		} catch (Exception e) {
			log.info(swipeError + e);
		}
		break;

i have used this co-ordinates but its still not working properly : scrollUp(0.6, 0.9, 0.5, 2000);

Try to calculate the coordinates in a different way.
I’m doing this calculation and work great on different devices with different screen sizes.

// calculate coordinates for vertical swipe
	int startVerticalY = (int) (size.height * 0.8);
	int endVerticalY = (int) (size.height * 0.21);
	int startVerticalX = (int) (size.width / 2.1);
	// calculate coordinates for horizontal swipe
	int startHorizontalX = (int) (size.width * 0.9);
	int endHorizontalX = (int) (size.width * 0.1);
	int startHorizontalY = (int) (size.height / 2.1);

In the result of this code i got these values 947-248-342,
648-72-563. So i changed the code to this but it is still not working. Can you fixing it?
public static void scrollUp (
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)248; //(size.height * startPercentage);
int endPoint = (int)947; //(size.height * finalPercentage);
getTouchAction().press(PointOption.point(anchor, startPoint))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
.moveTo(PointOption.point(anchor, endPoint)).release().perform();
}

Try to use this method without modifying it:

public void swipeDown(int howManySwipes) {
	Dimension size = driver.manage().window().getSize();
	// calculate coordinates for vertical swipe
	int startVerticalY = (int) (size.height * 0.8);
	int endVerticalY = (int) (size.height * 0.21);
	int startVerticalX = (int) (size.width / 2.1);
			try {
				for (int i = 1; i <= howManySwipes; i++) {
				new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
						.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, startVerticalY))
						.release().perform();
			}
		} catch (Exception e) {
			    				//print error or something
		}
		}

Replace the try/catch block with this if you want to swipe Up:

          try {
	      for (int i = 1; i <= howManySwipes; i++) {
					new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
							.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, endVerticalY)).release()
							.perform();
				}
			} catch (Exception e) {
				//print error or something
			}

I was getting some errors on “point”, “ofSeconds” and “waitOptions” so i have changed the code to this: and im calling it by swipeDown(1);
** scrollUp(1);**. But its not working so can you help?

public static void swipeDown(int howManySwipes) {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
// calculate coordinates for vertical swipe
int startVerticalY = (int) (size.height * 0.8);
int endVerticalY = (int) (size.height * 0.21);
int startVerticalX = (int) (size.width / 2.1);
try {
for (int i = 1; i <= howManySwipes; i++) {
new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, startVerticalY))
.release().perform();
}
} catch (Exception e) {
//print error or something
}
}

		public static void scrollUp(int howManySwipes) {
			org.openqa.selenium.Dimension size = driver.manage().window().getSize();
			// calculate coordinates for vertical swipe
			int startVerticalY = (int) (size.height * 0.8);
			int endVerticalY = (int) (size.height * 0.21);
			int startVerticalX = (int) (size.width / 2.1);
			  try {
			      for (int i = 1; i <= howManySwipes; i++) {
							new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
									.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, endVerticalY)).release()
									.perform();
						}
					} catch (Exception e) {
						//print error or something
					}
		}


		private static WaitOptions waitOptions(Object ofSeconds) {
			// TODO Auto-generated method stub
			return null;
		}


		private static Object ofSeconds(int i) {
			// TODO Auto-generated method stub
			return null;
		}


		private static PointOption point(int startVerticalX, int startVerticalY) {
			// TODO Auto-generated method stub
			return null;
		}


		
		}

Ok, in order to help you, first of all, what version of java client and appium do you have?

The errors for “point”, “ofSeconds” and “waitOptions” might be because you need to have latest versions 6.1.0 java and 1.9.1 Appium. Should be very easy solve with proper imports.

Second, what do you mean by not working? You got errors when the swipe starts? Or nothing happens when the swipe is performed?
From developer mode on your device activate the show tap and pointer location to see exactly if anything happens on the screen when swipe is performed

I get no errors at all while running it but nothing happens when the application opens. and secondly Im new to all this so can you tell me how to check the appium and java versions?

appium -v in cmd and java version in pom.xml file.
Just search for this in the forum there are topics already with these issue.
Follow a tutorial first