Page 1 of 1

How to use FindChildren to loop through a //div

Posted: Sun Jan 31, 2016 9:35 pm
by AdamMackintosh
Hello, I am looking for some help here. This is a bit more difficult than I can comprehend at the moment.

I have a div, and inside it, there are 10 divs:
div[@id='_ariaId_25']
div[@id='_ariaId_27']
div[@id='_ariaId_29']
div[@id='_ariaId_31']
etc

I'm trying to make a bit of user code to use FindChildren, count the divs, then loop through each one and for each one that is "[@class~'ms-bg-color-themeSecondary' and @visible='True']", I want to regex pull out the integer after the "_ariaId_" (ie: 25, 27, 29, 31) and store them in a variable comma separated.

Can anyone help me out with this?

I am attaching a screenshot. I am trying to populate a csv string variable with all the integers from those divs only, so that I can add this bit of code to another piece that loops through and clicks on each div. That part is already functional. Not all of those divs will have "[@class~'ms-bg-color-themeSecondary' and @visible='True']"

Thanks for any help,
Adam

Re: How to use FindChildren to loop through a //div

Posted: Mon Feb 01, 2016 9:10 am
by odklizec
Hi Adam,

Could you please post a Ranorex snapshot (not screenshot) of the div in question? Screenshot is unfortunately not very helpful, because there is no way for us to determine the best xpath for parent div you want to loop.

As for obtaining the number from '_ariaId_' string, you can use something like this"

Code: Select all

string idNumber = repo.yourdiv.element.GetAttributeValueText("Text", new Regex("_ariaId_(.*)"));
Where "Text" should be replaced with attribute containing the string you want to parse. And of course, you need to replace the repo path with element obtained via loop.

As for the loop, I believe you can use something like this (not tested,it's just an example ;))...

Code: Select all

// searches all divs under given div 
IList<Ranorex.DirTag> divList = Host.Local.Find<Ranorex.DivTag>("/pathtodiv[@controlname='name']/div",500);
IList<string> idNumberList = new List<string>();
string idNumber = "";
foreach (Ranorex.DivTag dvTag in divList)  
{  
	// returns number (as string) from given _ariaId_ label
	idNumber = dvTag.Element.GetAttributeValueText("Text", new Regex("_ariaId_(.*)"));
	// adds 'number' to list 
	idNumberList.Add(idNumber);
} 
// converts list to comma separated string
string commaSeparated = String.Join(",", idNumberList.ToArray());

Re: How to use FindChildren to loop through a //div

Posted: Mon Feb 01, 2016 5:14 pm
by AdamMackintosh
Before I post a snapshot, I added that code and ran it (missing ; and a typo in there), and it is returning

'System.Collections.Generic.IList<string>' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Collections.Generic.IList<string>' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\RanorexStudioProjects\test\Sample.UserCode.cs:49,58

The code looks good to me, thanks for showing it. The logic looks good, but I am extremely weak C# so I have no idea why ToArray() is melting.

Re: App get crashed while tap on element in iOS Instrumented app

Posted: Mon Feb 01, 2016 8:54 pm
by odklizec
I'm afraid, I'm not a C# professional too, but I think it should work? I would suggest to try to reinstall .net 3.5, eventually, try to compile your project with .net 4.0. But according to msdn, 'toarray' should be available in .net 3.5. If nothing helps, you might try to replace iList with List...

Code: Select all

List<string> idNumberList = new List<string>();

Re: How to use FindChildren to loop through a //div

Posted: Thu Feb 04, 2016 3:59 am
by log
Just in case anyone is interested, the ToArray() for System.Collections.Generic.IList<T> is a Linq extension method (https://msdn.microsoft.com/library/bb29 ... 00%29.aspx), since IList<T> is a subclass of IEnumerable<T>. So, you need to include

Code: Select all

using System.Linq;

System.Collections.Generic.List<T> has a ToArray() instance method (https://msdn.microsoft.com/en-us/librar ... 10%29.aspx).

Re: How to use FindChildren to loop through a //div

Posted: Thu Feb 04, 2016 8:05 am
by odklizec
That explains why it does not throw an error my side. I already have System.Linq included in my code module ;)