How to do scroll action till the particular element is present or not?

  1. I want to scroll down to till the particular web element is present and then I will assert particular element is present or not.
  2. I have tried with below coding for scrolling down the to reach the end of the page.
  3. Below code scrolls down, if it reaches the end of the page. It throws error.
  4. I need to do one scroll, after that i will check particular element is present or not, if that element is present means, I need to break from the loop.
  5. I have defined the loop in static manner as i<=10 times. How can I give that loop condition in dynamically?
  6. How can I do my scenario?

// scroll down action
for (int i = 0; i <= 10; i++) {
WebElement element = driver
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObjects = new HashMap<String, String>();
scrollObjects.put(“direction”, “down”);
((RemoteWebElement) element).getId());
js.executeScript(“mobile: scroll”, scrollObjects);
if (AppiumHelper.isElementPresent(driver,“Comment”)))

Error Message:-
An error occurred while executing user supplied JavaScript. (WARNING: The server did not provide any stacktrace information)(…)

First , use “mobile: scrollTo” .
Second it is possible that :

  1. there is no button with name “save_button” ( name should be exact same what you see on screen )
  2. there is multiple elements with same name.

Scroll to methods :

public static void scrollToElement(AndroidDriver driver, String elementName, boolean scrollDown){
	String listID = ((RemoteWebElement) driver.findElementByAndroidUIAutomator("new UiSelector().className(\"android.widget.ListView\")")).getId();
	String direction;
	if (scrollDown) {
		direction = "down";
	} else {
		direction = "up";
	HashMap<String, String> scrollObject = new HashMap<String, String>();
	scrollObject.put("direction", direction);
	scrollObject.put("element", listID);
	scrollObject.put("text", elementName);
	driver.executeScript("mobile: scrollTo", scrollObject);


public static void scrollToElement(IOSDriver driver,String elementName) {
	String targetCell = "//UIATableCell[UIAStaticText[@name=\""+elementName+"\"]]";
	WebElement cellWithText = driver.findElement(By.xpath(targetCell));
	HashMap<String, String> scrollObject = new HashMap<String, String>();
	scrollObject.put("element", ((RemoteWebElement)cellWithText).getId());
	driver.executeScript("mobile: scrollTo",scrollObject);
1 Like

@Sargis_Azaryan, I have tried with above code, I need one clarification , if an element is not located in that page, If I scroll down only that particular element is visible means, how can I able to scroll till that particular element using above method.It throws below error message.

Failed tests: An element could not be located on the page using the given search parameters.

You can scroll only to existing element even if it is not on screen initially.

@Sargis_Azaryan, I am having that particular web element in end of the page, Is there any scroll method to reach to that particular element?

and the method that i’ve written.

@Sargis_Azaryan, Using your method scrollToElement, i am able to scroll (In android), but it is not able to find and click the element

1 Like

You can’t scroll to invisible element in android. Yet scrollToElement for android that i provided should work , unless you had upgraded your Appium to 1.5 version

@Sargis_Azaryan, I am having (Ohiuchus) version. Please tell me how to upgrade to 1.5 version. Can you please share the staeps to do so. It will be really helpful.

Are you sure about upgrade ? I would not recommend , that fast.
Anyways …
In terminal window write

npm install -g appium

and press enter :slight_smile:

In recent update appium “mobile : scroll” deprecated, following code will work and video will help you to implement.

Scroll to text :

MobileElement radioGroup = (MobileElement) wd

.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"


"new UiSelector().text(\"+your_text+\"));");;

This link will help you :

You can use below mentioned method to swipe upward and call the method with a condition till that elemt gets displayed using driver.isdisplayed(). For ex:

swipeUpElement(driver, 5000);}

public void swipeUpElement(By by, int duration) throws InterruptedException{
int topY = dr.findElement(by).getLocation().getY();
int bottomY = topY+dr.findElement(by).getSize().getHeight();
int centerX = dr.findElement(by).getLocation().getX() + (dr.findElement(by).getSize().getWidth()/2);
dr.swipe(centerX, bottomY-40, centerX, topY,duration);

This Appium document resolved my issues with iOS scrolling and swiping and using both to position to a specific element:

// java
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“direction”, “down”);
scrollObject.put(“element”, ((RemoteWebElement) element).getId());
js.executeScript(“mobile: scroll”, scrollObject);

Thanks Scenario 1 works for me

Have you found any solution for this problem?

public static void scrollAndClick(AppiumDriver driver, By by) throws Exception {
boolean elementFoundAndClick = false;
if (driver instanceof AndroidDriver) {
// get screen dimensions
HashMap<String, Integer> scroll = getScrollStartandEndElements(driver);
for (int i = 0; i < 10; i++) {
driver.swipe(0, scroll.get(“start”), 0, scroll.get(“end”), 1000);
if (isElementPresent(driver, by)) {
waitElementToBeClickableByLocator(driver, by).click();
elementFoundAndClick = true;

public static HashMap<String, Integer> getScrollStartandEndElements(AppiumDriver driver) {
    Dimension dimensions = driver.manage().window().getSize();
    Double screenHeightStart = dimensions.getHeight() * 0.5;
    HashMap<String,Integer> scrollHash = new HashMap<>();
    Integer scrollStart = screenHeightStart.intValue();
    scrollHash.put("start", scrollStart);
    Double screenHeightEnd = dimensions.getHeight() * 0.2;
    Integer scrollEnd = screenHeightEnd.intValue();
    scrollHash.put("end", scrollEnd);

    return scrollHash;


public static void scrollToWebElement(AppiumDriver driver,
WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObjects = new HashMap<String, String>();
scrollObjects.put(“element”, ((RemoteWebElement) element).getId());
js.executeScript(“mobile: scrollTo”, scrollObjects);
try {
} catch (InterruptedException e) {

public static void scrollDown(AppiumDriver driver, WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObjects = new HashMap<String, String>();
    scrollObjects.put("direction", "down");
    scrollObjects.put("element", ((RemoteWebElement) element).getId());
    js.executeScript("mobile: scroll", scrollObjects);
    try {
    } catch (InterruptedException e) {

this is not working for android and ios. any one know any otherway