How to loop through rows in a table

Class library usage, coding and language questions.
markvanraalte
Posts: 13
Joined: Thu Apr 15, 2010 4:34 pm

How to loop through rows in a table

Post by markvanraalte » Thu Apr 15, 2010 4:52 pm

Hi

I am trying to automate a Java web app.
There is a table in the application (that I want to control) which contains a number of rows (with about 8 columns).

I wish to be able to loop through the rows in C# and then click on the first column if it contains a certain "innertext" value in the third column.

I am new to Ranorex and am not sure how to do this.

The Ranorex path for the first row (and first text column) is as follows:

"/dom[@caption='PureDeal']/body/div[@id='dragArea']/div[@id='divOpenPositions']/div[@id='ifrOpenPositionsHolder']/iframe/body/div/div/table/tbody[2]/tr[@id='yui-rec11']/td[2]/div/a[@innertext='CellTextHere']"

Now I can replace the expression: @id='yui-rec11' with the number 1 for first row
but I when I remove the innertext expression it no longer finds the cell.

I wish to loop thru all rows and read the values of innertext in columns 1 and 3. If the match a criteria I wish to click on column 1.

Please help
Thanks

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to loop through rows in a table

Post by Support Team » Fri Apr 16, 2010 11:01 am

Hi!

What your are looking for is the foreach statment in C#.
markvanraalte wrote:I wish to loop thru all rows and read the values of innertext in columns 1 and 3. If the match a criteria I wish to click on column 1.
You should code something like:
foreach (TrTag row in repo.Sample.TableBody.Find("./tr"))
{
   IList<TdTag> cells = row.Find<TdTag>("./td");
   if (cells.Count >= 3
         && cells[0].InnerText == "SomeValue"
         && cells[2].InnerText == "SomeOtherValue")
   {
      cells[0].Click();
   }
}
Regards,
Peter
Ranorex Support Team
.
Image

markvanraalte
Posts: 13
Joined: Thu Apr 15, 2010 4:34 pm

Re: How to loop through rows in a table

Post by markvanraalte » Fri Apr 16, 2010 7:14 pm

Thanks Peter

Thats really helpful.

Unfortunately I needed to reference tags that were lower down (within the /td element)

I solved it as follows:
foreach (TrTag row in repo.WebDocumentPureDeal.OpenPositionsWindow.TableBody.Find("./tr"))
{
IList<TdTag>cells = row.Find<TdTag>("./td");
ATag _securityCell = cells[0].Children[0].FindSingle("./a");
DivTag _positionCell = cells[2].FindSingle("./div");

if ("SomeValue" == _securityCell.InnerText
&& "AnotherValue" == _positionCell.InnerText)

etc

Presumably this is a reasonable thing to do? It seems to work fine.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to loop through rows in a table

Post by Support Team » Mon Apr 19, 2010 6:07 pm

markvanraalte wrote:Presumably this is a reasonable thing to do? It seems to work fine.
That's exactly how I would do it :-)

Regards,
Alex
Ranorex Support Team
.
Image