harry harry - 1 month ago 22x
Java Question

Appium [ iOS Native app ] - How to Scroll to a specific element/object using Java?

I want to scroll to a specific element which is not visible on the screen currently but it is down on the page somewhere, for which I have to scroll down. So

is not working and I have tried
jsExecutor.executeScript("mobile: scroll", scrollObject)
which is not working either. So any ideas for that?

If possible I want it Generic, such that it can search upwards as well as downwards where the object position is uncertain.


The solution is a work around. There is no exact function for this. First of all lets gather the requirements- Scroll page (with range from- [current page location, page End]) until given element is visible.

For scrolling we can use-

driver.swipe(startx, starty, startx, endy, 1000);

For finding element visibility we can use code like-

 try {
            dr.findElement(By.xpath(xpath); // find element with whatever Selector, I am using xpath
            if (dr.findElementsByXPath(xpath).size()>0 && dr.findElement(By.xpath(xpath)).isDisplayed()){
                System.out.println("Element found by xpath : " + xpath);
                return true;
            } else
                return false;
        } catch (NoSuchElementException e1) {
            System.out.println("Object not found");
            return false;
        } catch (Exception e2) {
            System.out.println("Unhandled Exception found");
            throw e2;

Now we have to put a condition, that scrolling should stop if last element is visible. For this we can modify the xpath of the current element, and check for visibility of the element found by that xpath, like-

String xpath_last = elementxpath.concat("/../*[last()]");  // if we are scrolling downwards then [last()]. But if we are scrolling up then make it [1]

This completes your condition. I guess this should do !