Issue getting cell count from CollectionViews() object


I am new to using Appium so please excuse my ‘newbie’ question.

Currently I am trying to automated a type of spread sheet GUI on iOS for an iPad application that me and my team are developing. This spread sheet is a CollectionViews() object which it identifiable using the the following xpath expression: “//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]”.

What I want to do is to get the cell count of this CollectionView so I tried messing around with the following code, but I have had no luck coming to a solution.
Also, if there is a way to extract/interact with CollectionViews and Cells directly using the Appium API that would be preferred rather than having to use the .executeScript method.

WebElement e = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]"));
int x = (Integer) ((JavascriptExecutor) driver).executeScript("arguments[0].cells().length;", e);

Thanks in advance for your help.

You shouldn’t be using executeScript, it doesn’t work that way on appium. It really only makes sense for webviews.

How about getting all your cells via appium, then counting them.

List<WebElement> elements = driver.findElements(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIATableCell"));
int numCells = elements.size()

Or something like that. driver.findElements is what you want.

Worked like a charm! Thanks a lot @jonahss, much appreciated!


1 Like