How to use FindChildren to loop through a //div

Best practices, code snippets for common functionality, examples, and guidelines.
AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

How to use FindChildren to loop through a //div

Post by AdamMackintosh » Sun Jan 31, 2016 9:35 pm

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
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Mon Feb 01, 2016 9:10 am

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());
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

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

Post by AdamMackintosh » Mon Feb 01, 2016 5:14 pm

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.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Mon Feb 01, 2016 8:54 pm

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>();
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

log
Certified Professional
Certified Professional
Posts: 14
Joined: Tue Mar 24, 2015 6:28 am
Location: Sydney, Australia

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

Post by log » Thu Feb 04, 2016 3:59 am

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).

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

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

Post by odklizec » Thu Feb 04, 2016 8:05 am

That explains why it does not throw an error my side. I already have System.Linq included in my code module ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration