How to select the first element from a UIACollectionView

I need to select,click the first item from a list displayed after a search, the class is UIACollectionView for an iOS test, I attach you an screenshot of the structure

Right now Im using the item name but this can change, so thats why I need to do it in other way

Thank you

driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeCollectionView[`name == \"elementID_if_you_have\"`]/**/XCUIElementTypeStaticText[1]"));
// or
driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeCollectionView/**/XCUIElementTypeStaticText[1]"));
// replace `XCUIElementTypeStaticText` with your element type of 'ITEM 1'

Good morning @Aleksei, thanks for your answer, so I’m triying your solution but Im having the next issue
Eclipse say “AppiumBy cannot be resolved”, how can I fix that issue? thank you

And for the item 1 I have the XCElementType = XCUIElementTypeCell, this one is the element type that you mean?

Thank you

if you are with older java version it was MobileBy

yes: XCUIElementTypeCell replace with it

Same, cannot be resolved, maybe Im missing some import?
Tests file
57
BaseTest file

import io.appium.java_client.MobileBy;
1 Like

ok, now is working the MobileBy!

Now when I run the test the problem is the “ios class chain”

Im using this line: driver.findElement(MobileBy. iOSClassChain (“/XCUIElementTypeCollectionView//XCUIElementTypeCell[1]”)).click();

And the log say:
org.openqa.selenium.InvalidSelectorException: invalid selector (Locator Strategy ‘-ios class chain’ is not supported for this session) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

My code

tests file

package tests;

import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import io.appium.java_client.MobileBy;

public class PROES_Searchitem extends BaseTest {

public PROES_Searchitem() throws MalformedURLException {
super();
// TODO Auto-generated constructor stub
}

@Test

public void probarseleccionararticulosinnombre() {

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. xpath (“//*[@text=‘Busca por producto, colección o palabra clave’]”))).click();

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. name (“search_searchTextField”))).click();

driver.findElement(By. name (“search_searchTextField”)).sendKeys(“vestido”);

driver.findElement(By. name (“buscar”)).click();

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. name (“Filtros”)));

driver.findElement(MobileBy. iOSClassChain (“/XCUIElementTypeCollectionView//XCUIElementTypeCell[1]”)).click(); // replace XCUIElementTypeStaticText with your element type of ‘ITEM 1’

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. id (“productDetail_buySelectSizeButton”))).click();

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. id (“productDetail_buySelectSizeButton”))).click();

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. id (“Tramitar pedido”))).click();

new WebDriverWait(driver, 30).until(ExpectedConditions. presenceOfElementLocated (By. name (“TRAMITAR PEDIDO”)));

}

@AfterMethod

public void tearDown() {

driver.quit();

}

}

baseTest file

package tests;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;

public class BaseTest {
private String reportDirectory = “reports”;
private String reportFormat = “xml”;
private String testName = “PROES-Searchitem”;
protected IOSDriver driver = null;

public BaseTest() throws MalformedURLException {

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(“reportDirectory”, reportDirectory);
dc.setCapability(“reportFormat”, reportFormat);
dc.setCapability(“testName”, testName);
dc.setCapability(MobileCapabilityType.UDID, “E4F6EDC4-E23C-42B3-987D-3DB282CA77C1”);
dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID, “com.app.beta.test.testapps”);
driver = new IOSDriver<>(new URL(“http://localhost:4723/wd/hub”), dc);
driver.setLogLevel(Level.INFO);

}
}

good morning @Aleksei, can you help me with that invalid selector? thank you

try with:

"**/XCUIElementTypeCollectionView/**/XCUIElementTypeCell[1]"

Im having this exception: An exception occurred: org.openqa.selenium.InvalidSelectorException

compare what i wrote and your text. you missing **