Search code examples
javaappiumappium-androidappium-ios

Looking to ios and android scroll up and down till element found and then click


I tried lot of blogs and discussion about ios & android scrolling in appium, java. but looks like most of method are deprecated or not working. pls helping. I am using appium 1.15.1, java, selenium.

looking for quick help.

I am trying following code but it's scrolling twice and not even clicking.

Hi,

I am on Mac and working with real device. I am using Appium 1.15.1, Selenium, Java with POM. I wrote following code for iOS swipe down.

Hi,

public void chkMenuOptioniOS1(String sName)
    {   
/* This is actual path which got from inspector
//XCUIElementTypeStaticText[@name="Account Information"]
*/
        String targetCell = "//XCUIElementTypeStaticText[contains(@name,'" + sName + "')]";
        MobileElement cellWithText = driver.findElement(By.xpath(targetCell));
        HashMap<String, String> scrollObject = new HashMap<String, String>();
        scrollObject.put("element", ((RemoteWebElement)cellWithText).getId());
        scrollObject.put("direction", "down");
        scrollObject.put("predicateString", "value == '" + sName + "'");
        scrollObject.put("toVisible", "true");
        driver.executeScript("mobile:scroll", scrollObject);
        try {
            System.out.println("In Try Block "+cellWithText.getText());
            Thread.sleep(10);
            cellWithText.click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  1. It scrolling twice
  2. and it;s not clicking,

Attaching DOM object screen capture.

Please help meenter image description here


Solution

  • These are the methods for your needs for iOS and Android:

    private JavascriptExecutor js;
    private HashMap<String, String> scrollObject = new HashMap<>();
    
    void iosScrollToAnElement(IOSElement el) {
            scrollObject.put("direction", "down");
            scrollObject.put("element", el.getId());
            js.executeScript("mobile: swipe", scrollObject);
    }
    
    void androidScrollToAnElementByText(String text) {
            try {
                ((AndroidDriver)driver).findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0))");
            } catch (Exception e) {
                throw new NoSuchElementException("No element" + e);
            }
        }
    

    after this you need to click() on your particular element that you are looking for. (there methods only for scrolling)