Vignesh
Vignesh

Reputation: 165

How to use css selector instead of xpath in appium 1.2.2 version for ios application

I want to click inside collectioncell and i have inspected the xpath as

//UIAApplication[1]/UIAWindow[1]/UIAScrollView[3]/UIACollectionView[1]/UIACollectionCell[1]

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[3]/UIACollectionView[1]/UIACollectionCell[1]")).click();`

but the above xpath is not working for me. I am using Appium1.2.2 version in mac. How to modify the above xpath in terms of css selector?.

Upvotes: 1

Views: 8805

Answers (1)

Jess
Jess

Reputation: 3146

It's likely not your XPath. Appium + IOS + XPath is known to have issues. I suggest you use the UIAutomation part of my answer, but I also provided an alternative XPath solution which may work.


In Appium there is no concept of "CSS Selector". Native Apps are not written in HTML.

The closest solution to By.cssSelector would be to do:

By.className("UIACollectionCell")

However, that would give you all collection cells, regardless of if they belonged to multiple parent UIACollectionViews. That's not what we want.

We want to find all child elements relative to a single parent element

So the locator strategy you need needs to be able to filter elements relative to a parent element. We have two options: XPath and UIAutomation

XPath

value = "//UIACollectionView[1]/UIACollectionCell"
collectionCells = driver.findElement(By.xpath(value);

UIAutomation

The benefit is that UIAutomation is native, quick, and reliable. XPath will sometimes give you completely different elements, or Instruments will just flake out and crash.

I am assuming you are not using Appium's Java Client library. To use the UIAutomation locator strategy, you need to use Appium's Java Client library.

With the UIAutomation locator strategy, we would solve your problem like so:

value = ".collectionViews()[0].cells()"
collectionCells = driver.findElementsByIosUIAutomation(value)

Upvotes: 3

Related Questions