Page 1 of 1

Access table rows

Posted: Thu Feb 21, 2013 10:55 am
by LynchDev
I have a <table> on a webpage that can have a variable amount of table rows, and I would like to know how I could iterate through these and pull out the data inside each.

In addition, the table's ID tag is a GUID, so essentially the table has no identifying information. So the only way I can access the table is to keep a reference to the first row in the Ranorex repository, then backtrack to the table tag and go from there.

The current XPath I am using:

Code: Select all

//table[@id~'.*']/tbody/tr[1]/td[@title~'.*(aaa|bbb).*']
As you can see I am skipping over the table id with a regex, and then finding the first row with a regex that matches data with 'aaa' or 'bbb'.

To sum up, some pseudo code for what I am trying to do:

Code: Select all

if (repo.tableData.Exists)
{
    var table = repo.tableData.ParentRow.ParentTable;
    var listOfInvalidCellsFound;
    loop (rows in table)
    {
         loop (cells in row)
         {
             if (aaabbbRegex.IsMatch(cell.Text))
                 listOfInvalidCellsFound.Add(cell.Text);
         }
    }
}
Is this possible with the API?

ps. I'm open to using other ideas, but this is the only way I found that might work (new to Ranorex API)

Re: Access table rows

Posted: Mon Feb 25, 2013 6:52 pm
by Support Team
Hello,

I would use the following code to iterate over cells in a table.
Please take a look at it and adjust it to meet your needs.
var rows = Host.Local.Find<TrTag>("/dom[@domain='www.ranorex.com' and @caption='Ranorex Test Page']//table/tbody/tr/td[@innertext~'1']/../../tr");
int rowNum = 1;

foreach (var row in rows)
{
	Report.Info("===========================");
	Report.Info("Row: " + rowNum);				
	var cells = row.FindDescendants<TdTag>();
				
	foreach (var cell in cells)
	{
		Report.Info("Cell: " + cell.InnerText);
	}
	rowNum++;				
}

Regards,
Markus (T)