scrollTo (String text) method of Android Driver not working

Using the scrollTo(String text) and scrollToExact(String text) method of Android Driver. However the scrolling is done for the entire contact list first downwards and then upwards but it does not scroll upto the string text provided and in the end throws error-
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

Code below:

public class test2 {

//public WebDriver driver;
public AppiumDriver driver1;

 @BeforeClass
 public void setUp() throws MalformedURLException{
	 
	//Set up desired capabilities and pass the Android app-activity and app-package to Appium
	 
	 DesiredCapabilities capabilities = new DesiredCapabilities();
	    capabilities.setCapability("platformName", "Android");
	    capabilities.setCapability("platformVersion", "5.0
	    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
	    capabilities.setCapability("deviceName", "4763f751");
        capabilities.setCapability("device", "Android");
        capabilities.setCapability("appPackage", "com.android.contacts");             
        capabilities.setCapability("appActivity", "com.android.contacts.activities.PeopleActivity");
              
      //Create RemoteWebDriver instance and connect to the Appium server.
        driver1 = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), capabilities);    

 }
 
 @Test
 
 public void testCal()  throws Exception {
	 
		WebElement clk=driver1.findElement(By.name("some contact name here"));		
		String txt= clk.getText();
		 driver1.scrollTo(txt);
						
	 }

@AfterClass
public void teardown(){
//close the app
//driver1.quit();
}

}

pls help on how to overcome this thing

See ScrollTo and ScrollToExact function is not working

checked the post … want to know one thing where to make these changes.

public MobileElement scrollTo(String text) {
String uiScrollables = AndroidDriver.UiScrollable(“new UiSelector().textContains(”" + text + “”)");
return (MobileElement) findElementByAndroidUIAutomator(uiScrollables);
}

Create a library function in your source tree and call it whereever you would call the Appium funciton.

Feel free to try the following approach as well, it works 100% for me in prod environment.

public void scrollTo(String selector) {
    String selectorString = String.format("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("+ selector +")");

 driver.findElement(MobileBy.AndroidUIAutomator(selectorString));
}

The difference is that I’m passing both UiScrollable and UiSelector as a string, rather than use the AndroidDriver’s UiScrollable on a UiSelector string.

Tried this approach…however i m runnig into exception error.

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
*** Element info: {Using=-android uiautomator, value=new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(Ambulance)}
Session ID: 7141c660-5aae-46ba-a0e8-1d52702f210a
Driver info: io.appium.java_client.android.AndroidDriver

you need to pass a UiSelector, not a string.

so instead of Ambulance, pass " new UiSelector().text(“Ambulance”) "

or

replace

String selectorString = String.format(“new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(”+ selector +")");

with

String selectorString = String.format(“new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(”+ selector +"))");

But that limits your searching to text only. With the previous approach you can pick what you want ot search for, be it content description, class, resource id, child of a selector, parent etc.

Tried both approach and now i get this error.

org.openqa.selenium.UnsupportedCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.

*** Element info: {Using=-android uiautomator, value=new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(Ambulance))}
Session ID: 7be17a44-a126-477a-b8b9-dffd6222d6f0
Driver info: io.appium.java_client.android.AndroidDriver

are you sure there’s a scrollable element on the screen?

Yes.
i m trying to open contacts App on my android device and want to scroll upto one of the contact name.

earlier when i used the Android Driver Api Method- scrollTo(String text), it was getting scrolled continuously first downwards then upwards and then through exception after 2 min.
org.openqa.selenium.NoSuchElementException.

so tried your approach but still i m not able to get this done

Following is end to end code you can try, Just change the name that you are looking:

public class scrollToForElement 
{
        public AndroidDriver driver1;

         @BeforeClass
         public void setUp()
         {
            //Set up desired capabilities and pass the Android app-activity and app-package to Appium

             DesiredCapabilities capabilities = new DesiredCapabilities();
        
                capabilities.setCapability("deviceName", "Testing");
                capabilities.setCapability("appPackage", "com.android.contacts");             
                capabilities.setCapability("appActivity", "com.android.contacts.activities.PeopleActivity");

              //Create RemoteWebDriver instance and connect to the Appium server.
                try
                {
                    driver1 = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), capabilities);    
                    driver1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
         }
         
         @Test
         public void testCal()  throws Exception 
         {
                 String str="Franklin";
//                driver1.scrollTo(str);
                
                 driver1.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+str+"\").instance(0))");
                
                driver1.quit();
        }
}
5 Likes

ohh WoW !!!
This works as i wanted it to.
Thanks a lot :smile:
And thanks to everyone for their inputs, this is really learning for me :slight_smile:

heyall,
I’m still facing this issue, instead scroll its clicking on dropdown element.

i tried the followings

  1.     dr.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+symbol+"\").instance(0))");
    
  2. driver.scrolltoexact(“Text”);

  3. HashMap<String, String> scrollObject = new HashMap<String, String>();
    RemoteWebElement element = (RemoteWebElement)dr.findElementByAndroidUIAutomator(“new UiSelector().className(“android.widget.ListView”)”);
    JavascriptExecutor js = (JavascriptExecutor) dr;
    String webElementId = ((RemoteWebElement) element).getId();
    scrollObject.put(“text”, symbol);
    scrollObject.put(“element”, webElementId);
    js.executeScript(“mobile: scrollTo”, scrollObject);

none of the above are working fine for me…

Please provide the appium server log so we can better understand the failure

please find the script and logs
java script:
dr.scrollToExact(symbol);
dr.switchTo().activeElement().findElement(By.id(“com.msf.opx:id/txtPosSymbolDesc”));
List list1 = dr.findElements(By.id(“com.msf.opx:id/txtPosSymbolDesc”));
for (WebElement cr : list1) {
if (cr.getText().contains(symbol)) {
cr.click();
}
}

please find the script and logs
java script:
dr.scrollToExact(symbol);
dr.switchTo().activeElement().findElement(By.id(“com.msf.opx:id/txtPosSymbolDesc”));
List list1 = dr.findElements(By.id(“com.msf.opx:id/txtPosSymbolDesc”));
for (WebElement cr : list1) {
if (cr.getText().contains(symbol)) {
cr.click();
}
}

appium logs:

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:7,“value”:“No element found”}
info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“id”,“selector”:“com.msf.opx:id/btnpositions”,“context”:“”,“multiple”:false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“com.msf.opx:id/btnpositions”,“context”:“”,“multiple”:false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding com.msf.opx:id/btnpositions using ID with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.msf.opx:id/btnpositions]
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:{“ELEMENT”:“4”}}
info: [debug] Responding to client with success: {“status”:0,“value”:{“ELEMENT”:“4”},“sessionId”:“9073d745-390b-49ce-a414-fc4d57cf78d1”}
info: ← POST /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1/element 200 3555.858 ms - 87 {“status”:0,“value”:{“ELEMENT”:“4”},“sessionId”:“9073d745-390b-49ce-a414-fc4d57cf78d1”}
info: → POST /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1/element/4/click {“id”:“4”}
info: [debug] Pushing command to appium work queue: [“element:click”,{“elementId”:“4”}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“4”}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:true}
info: [debug] Responding to client with success: {“status”:0,“value”:true,“sessionId”:“9073d745-390b-49ce-a414-fc4d57cf78d1”}
info: ← POST /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1/element/4/click 200 2132.285 ms - 76 {“status”:0,“value”:true,“sessionId”:“9073d745-390b-49ce-a414-fc4d57cf78d1”}
info: → POST /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1/element {“using”:“-android uiautomator”,“value”:"new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().description("GE JAN16 > info: [debug] [BOOTSTRAP] [debug] Parsing scrollable: new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(“GE JAN16 20 CALL”).instance(0))
info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: boolean arg: true
info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: int arg: 0
info: [debug] [BOOTSTRAP] [debug] UiScrollable invoking method: public boolean com.android.uiautomator.core.UiScrollable.scrollIntoView(com.android.uiautomator.core.UiObject) throws com.android.uiautomator.core.UiObjectNotFoundException args: new UiSelector().text(“GE JAN16 20 CALL”).instance(0),
info: [debug] [BOOTSTRAP] [debug] UiScrollable coerce type: class com.android.uiautomator.core.UiObject arg: new UiSelector().text(“GE JAN16 20 CALL”).instance(0)
info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: “GE JAN16 20 CALL”
info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: int arg: 0
info: [debug] [BOOTSTRAP] [debug] Method name: scrollIntoView
info: [debug] [BOOTSTRAP] [debug] Setting uiObject for scrollIntoView
info: [debug] [BOOTSTRAP] [debug] Invoking method: public boolean com.android.uiautomator.core.UiScrollable.scrollIntoView(com.android.uiautomator.core.UiObject) throws com.android.uiautomator.core.UiObjectNotFoundException with: com.android.uiautomator.core.UiObject@1c1d2bca
info: [debug] [BOOTSTRAP] [debug] Invoke complete.
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=GE JAN16 20 CALL, INSTANCE=0]
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[TEXT=GE JAN16 20 CALL, INSTANCE=0]
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:7,“value”:“No element found”}
info: [debug] Condition unmet after 63589ms. Timing out.
info: [debug] Responding to client with error: {“status”:7,“value”:{“message”:“An element could not be located on the page using the given search parameters.”,“origValue”:“No element found”},“sessionId”:“9073d745-390b-49ce-a414-fc4d57cf78d1”}
info: ← POST /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1/element 500 63523.488 ms - 195
info: → DELETE /wd/hub/session/9073d745-390b-49ce-a414-fc4d57cf78d1 {}

This Worked For Me :smile:
Thanks

Thank you! Worked perfectly for me!
Could you explain step by step this line

I hope you know this, Android automation is possible by UIAutomatior. The following is the statement in Android Automator

new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains("“+str+”").instance(0))

The same is passed in driver.findByUIAutomator.

@UD

What is the iOS alternative for scrollTo() method ?