How to scroll the list view and read out all the list items in Android using Java

4,108 views
Skip to first unread message

Shiva Ghanate

unread,
Apr 23, 2014, 7:03:23 AM4/23/14
to appium-...@googlegroups.com
List<WebElement> mylist = driver.findElements(By.id("com.androidexample.listview:id/list"));
//How to scroll after this in Java.
 
I can't find anything like driver.scroll() method.
 
Also the list has more than 8 elements, so how do I get the list of all the elements in the whole list.
 
Once I get the length of the list I am aiming to run a for loop and check every item for the content in them.
ListView1.jpg

artavazd....@picsart.com

unread,
Apr 23, 2014, 8:57:33 AM4/23/14
to appium-...@googlegroups.com
Hi Shiva,

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);

Shiva Ghanate

unread,
Apr 25, 2014, 3:46:54 AM4/25/14
to appium-...@googlegroups.com
I am new to Java coding.
I saw this code, but was not able to implement it for my list view,
 
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "down");
scrollObject.put("element", ((RemoteWebElement) element).getId());
 
//when i try to give my mylist as the element, it doesnt work for me. 
js.executeScript("mobile: scroll", scrollObject);
 
Can you help me with that.??

Shiva Ghanate

unread,
Apr 25, 2014, 3:47:30 AM4/25/14
to appium-...@googlegroups.com
It would be helpful if you can explain me how does the code snippet works.

Shiva Ghanate

unread,
Apr 25, 2014, 4:21:37 AM4/25/14
to appium-...@googlegroups.com
Hi,
 
I have modified the code a little, but still not able to scroll.
 
WebElement mylist= driver.findElement(By.id("com.androidexample.listview:id/list"));
    
// Code for scrolling the lt

     JavascriptExecutor js = (JavascriptExecutor) driver;
     HashMap<String, String> scrollObject = new HashMap<String, String>();
     scrollObject.put("direction", "down");
     scrollObject.put("element", mylist.getText());

     js.executeScript("mobile: scroll", scrollObject);
 
Error what I get : org.openqa.selenium.WebDriverException: ERROR running Appium command: string is not a function
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

On Wednesday, April 23, 2014 6:27:33 PM UTC+5:30, artavazd....@picsart.com wrote:

Shiva Ghanate

unread,
Apr 25, 2014, 7:29:16 AM4/25/14
to appium-...@googlegroups.com
Hi I used this code, which works for scrolling the list, but then it throws an error.

WebElement mylist= driver.findElement(By.id("com.androidexample.listview:id/list"));
// Code for scrolling the list
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "down");
scrollObject.put("element", ((RemoteWebElement) mylist).getId());
js.executeScript("mobile: scrollTo", scrollObject);
System.out.println("Scrolling worked");

Scrolls the screen and then it throws error:

Appium Error log:

> [36minfo[39m: Pushing command to appium work queue relementscrollTo.

rtext”:null:direction”:”down:elemenilcr:”51]

> ERROR: [9Orndebug[39m: Appium request initiated at /wd/hublsessionlO74be83f-c086--488b-

922-7e544b7d3be2/execute

> ERROR: [9Orndebug[39m: Request received with params: Çargs’:

[{“elernent”:”5,direction”:down],Mscript”:”mobile: scrol ITo”)

> [36minfo[39m: [BOOTSTRAP] [info] Cot data from client

> [36minfo[39m: [BOOTSTRAP] [info] Cot command of type ACTION

> [36minfo[39m: [BOOTSTRAP] [debug] Cot command action: scrollTo

> [36minfo[39m: [BOOTSTRAP] [info] Returning result rvalue:Could not scroll element into view:

null”statuC:13)

> [36m info[39m: Responding to client with error rstatus”:13,”valueTmessage:An unknown server-

side error occurred while processing the command.”origValu&:”Could not scroll element into view:

> POST /wd/hub/sessionfO74be83f-c086-488b-a922-7e544b7d3be2/execute 500 7574ms - 237b

> [36m info[39m: Didn’t get a new command in 60 secs, shutting down...



Eclipse Console Log: 

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 7.60 seconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
Session ID: 074be83f-c086-488b-a922-7e544b7d3be2
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{platformVersion=4.1, platform=XP, databaseEnabled=false, javascriptEnabled=true, browserName=Android, app-package=TMS_Mobile.TMS_Mobile, webStorageEnabled=false, device=Android, app-activity=tms.mobile.ui.android.Login, desired={platform=Windows, browserName=Android, app-package=TMS_Mobile.TMS_Mobile, device=Android, app-activity=tms.mobile.ui.android.Login, version=4.4.2}, warnings={}, takesScreenshot=true, version=4.4.2}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:504)
at default_package.Android_Test.TaskList(Android_Test.java:138)
at default_package.Android_Test.main(Android_Test.java:49)




On Wednesday, April 23, 2014 6:27:33 PM UTC+5:30, artavazd....@picsart.com wrote:

Brian Rock

unread,
Jul 27, 2014, 2:33:40 PM7/27/14
to appium-...@googlegroups.com
This is an old thread, but hopefully this helps someone :) 

I ran into this same error, you just need to add in one more element to your HashMap, that is the text you want to scroll to.  This is my code that worked. 

MobileElement element = (MobileElement) DeviceWrapper.getAppiumDriver().findElementByClassName(locator);
JavascriptExecutor js = (JavascriptExecutor) DeviceWrapper.getAppiumDriver();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject
.put("direction", "down");
scrollObject
.put("element", ((RemoteWebElement) element).getId());

scrollObject
.put("text", text);
js
.executeScript("mobile: scrollTo", scrollObject);


And here is the code from the Controller.js file that it's actually calling.  

exports.mobileScrollTo = function (req, res) {
  logCustomDeprecationWarning
('mobile method', 'scrollTo',
     
"scrollTo will be removed in a future version of appium");
  req
.body = _.defaults(req.body, {
    element
: null
 
, text: null
 
, direction: "vertical"
 
});

 
var element = req.body.element
   
, text = req.body.text
   
, direction = req.body.direction;
  req
.device.scrollTo(element, text, direction, getResponseHandler(req, res));
};
Reply all
Reply to author
Forward
0 new messages