@Bilal_Jarwan swipe function already deprecated from the 1.6.5 version onwards.so54 this function is not going to work.For swiping you have to use the TouchAction class.
@suryakanta939 its working for me, that’s why i suggest this solution
@Bilal_Jarwan yes it will work but for the appium version bellow 1.6.5 .But question was for appium 1.7
Thanks:slightly_smiling_face:
yes, that’s right thanks, also i have set the solution for the main issue
how to swipe until you find you element.
Refer to https://discuss.appium.io/t/swipe-scroll-best-practice-with-java-client-5/18589/17982 for consolidated answers
@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.height0.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.height0.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
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);
}
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;
}
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
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.
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:
-
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
-
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.