Unable to get 'UIATableCell' count of 'UIATableView' in iOS

I’ve given you the logic you can use: Find the table, using find_elements_by_class_name find all of it’s cell children. You simply need to rewrite it in Java.

1 Like

All answers are relevant but for your specific requirement try this code

List WebElement elements= driver.findElementsByXPath("//UIATableCell")
elements.size() ----> this will give you the count.

Had to skip <> in the above ex. coz the editor is skipping anything that is between < and >. First post on Appium forum :slight_smile:

Thanks Kiran,

I will try this code and let you know…

I am using the below code, but I am not getting correct cells in CollectionView

List elements1 = wd.findElements(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIACollectionCell"));
int numCells1 = elements1.size();

If there are 2 cells, I am getting output as 1 cell.

What is the page source XML?

Sorry I didn’t get you Graham.

Step1: Get the page source for the application you’re testing by opening the Appium Inspector and clicking the “Copy XML” button.
Step2: Open this online XPATH tester tool: http://www.freeformatter.com/xpath-tester.html and paste your page source XML into the “XML Input” section of the tool.
Step3: Paste your XPATH expression //UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIACollectionCell into “XPath expression” section of the tool.
Step4: Click the “Text XPATH” button to see the results of your proposed XPATH search

This is a much faster way of developing successful XPATH than running your test scripts over-and-over. It will show you which elements match your XPATH expression. It will describe any gross errors you’re experiencing, like an inability to compile your XPATH expression (not your current issue). And will help you learn how to visually parse the XML for data and relationships, which will further help you develop your XPATH skills.

I have several UIAStaticText Elements under each UIATableCell.

Can u tell me how i can iterate through the each of the TableCell and retrieve all the UIAStaticText

One solution in Python:

# Find your table, xpath in this example, use whatever locator strategy you want
table = self.driver.find_element_by_xpath(table_xpath)
# Find cells which are children of the table, note Appium doesn't support xpath for the children
cells = table.find_elements_by_class_name('UIATableCell')
# Iterate through the cells and obtain their text
for cell in cells:
	# Find text elements which are children of the cell, note Appium doesn't support xpath for the children
	current_cell_texts = cell.find_elements_by_class_name('UIAStaticText')
	# Do something with the text elements, store them, iterate through them, etc.

Hi,

i tried this…it worked…
List tableCells = (List) driver.findElementsByXPath("//UIAWindow[1]/UIATableView[3]/UIATableCell[@visible =‘true’]");
for(int i=0;i<tableCells.size();i++)
{
elementInTableCells = tableCells.get(i).findElements(By.xpath("//UIAStaticText"));
}

Hi Telmo,

I am facing an issue for validating a table row contents

I want to validate whether the value in a column are in ascending or descending order.

Could you please let me know how to do it? I am new to Ruby and using Ruby Capybara.

Hello bhaskar,

The below code works for me.

if (driver.findelement(tableview xcpath)!=null) {
int size = driver.findelement(tableview xcpath).findElementsByClassName(“UIATableCell”).size();
System.out.println(“The size”+size);
for (int i=0;i<=size;i++){
row = driver.findelement(tableview xcpath).findElementsByClassName(“UIATableCell”).get(i);
String name = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell["+i+1+"]/UIAStaticText[2]")).getText();
}
}

1 Like

Hi Telmo,

I am using iOS native app to automate. I have a table with rows and each rows does not have a column. But The columns need to be split based on space. When I split columns it is creating issue. Let me show you how it looks

Please let me know if there any solution for this. I cannot split based on space because second column data has a space and this creates problem.

Regards
AJ

Well, you can try:

String[] arrayStrings = yourRow.split(" ")

arrayStrings[0] will be the first column
arrayStrings[arrayStrings.size()-1]  will be the last column
arrayStrings[arrayStrings.size()-2]  will be the last -1 column

and so on until you get last 5 elements. The rest of strings (you can know them knowing the .size() - 5 and less the first one) are the second column

Hi telmo

I tried the same approach here but the thing is second column which has “Click & Collect” and "Standard Delivery " also get split and every row will give different column count and this is causing the issue.

Any concrete solution for this?

My solution works for you :slight_smile:

Try this horrible piece of code:

class Appium {
    public static void main(String args[]) {
        String finalValues[] = new String[7];
        String yourRow = "10086628 Standard ui Delivery Open L2 1 1 0";
        String[] arrayStrings = yourRow.split(" ");
        finalValues[0] = arrayStrings[0];

        int incr=1;
        for (int i=(finalValues.length-1);i>1;i--) {
            finalValues[i] = arrayStrings[arrayStrings.length-incr];
            incr++;
        }

        finalValues[1] = "";
        while (incr<arrayStrings.length) {
            finalValues[1] = arrayStrings[arrayStrings.length - incr]+ " " + finalValues[1];
            incr++;
        }

        //print result
        for (int i=0;i<finalValues.length;i++) {
            System.out.println("Column "+(i+1)+": "+finalValues[i]);
        }
    }
}

That will result in:

Column 1: 10086628
Column 2: Standard ui Delivery 
Column 3: Open
Column 4: L2
Column 5: 1
Column 6: 1
Column 7: 0
1 Like

Hi Telmo, thank you so all your solutions. it is working fine for me.

I am facing one more problem. When I want to validate whether the data is value1 or value 2 I am unable to run it.
here is the below code and unfortunately expect is not working here. I am not sure what is the issue here

totalRows=batchlistPage.getRowCount
rowNum=1
while(rowNum<=totalRows)
data = batchlistPage.getRowText(rowNum)
puts data[3]
puts value1
puts value2
expect(data[3]).to eq(value1) | eq(value2)
//ABOVE LINE IS NOT EXECUTING AND IT COMES UP WITH FAIL MESSAGE
# expect(data[3].to eql? value1 || (data[3].to eql? value2)

  rowNum+=1
  end

This code worked for me actually. Thanx

hi
Below code is working for me ,
int size=driver.findElement(By.xpath(“here specify your xpath fo UIATable”).findElements( By.className(“UIATableCell”)).size();

Try that code .

Hi Madhumita,

I tried your code but it’s not working for me. Saying Cast is redundant. How did you implement it :slight_smile: