get item count and item text for an unordered list?
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
get item count and item text for an unordered list?
<deleted>
Last edited by godzillajoe on Tue Mar 11, 2014 6:45 pm, edited 1 time in total.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: get item count and item text for an unordered list?
Hello godzillajoe,
Please take a look at code snippet below to get an idea how to do that:
Markus (T)
Please take a look at code snippet below to get an idea how to do that:
// get a list of all items var itemList = Host.Local.Find<Ranorex.LiTag>("/dom[@domain='www.ranorex.com' and @caption='Ranorex Test Page']//div[#'Content']//ul/li"); Report.Info(itemList.Count.ToString()); foreach (var item in itemList) { Report.Info(item.InnerText); // get name of item }Regards,
Markus (T)
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
Thanks, I did it a slightly different way but eventually figured it out.
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
So, as a followup, I have another list where the A tag with the text I want is buried a few levels deep under the Li tag. I tried to modify the code to "FIND" the A tag but that doesn't seem to work.
Find<Ranorex.ATag> returns an empty list
I want to grab that 1st Choice text under the P tag and all the other ones down the tree.
Do I have to do a nested FIND at each "level"
Find<Ranorex.ATag> returns an empty list
I want to grab that 1st Choice text under the P tag and all the other ones down the tree.
Do I have to do a nested FIND at each "level"
Last edited by godzillajoe on Tue Mar 11, 2014 6:47 pm, edited 1 time in total.
Re: get item count and item text for an unordered list?
You should be able to do one find at the level you want, but you need to pass the RanorexPath from the object you are searching FROM to the object you are searching FOR.
So, for your example, assuming you are searching FROM the top Li tag (TopLi here):
That should return every object under TopLi that matches the path you gave it. Notice that the path you give starts where the TopLi path "ends", so to speak.
You can (and probably should) modify the path to be more specific so it takes less time and finds only the things you actually want.
So, for your example, assuming you are searching FROM the top Li tag (TopLi here):
Code: Select all
TopLi.Find<ATag>("./div/div/p/a");
You can (and probably should) modify the path to be more specific so it takes less time and finds only the things you actually want.
Shortcuts usually aren't...
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
<deleted>
Last edited by godzillajoe on Tue Mar 11, 2014 6:48 pm, edited 1 time in total.
Re: get item count and item text for an unordered list?
You need a common ancestor (named "root" here). Try something like this (working off the top of my head, so this is somewhat pseudo-code):
Code: Select all
LiTag root = Host.Local.Find<Ranorex.ATag>("/dom[@domain='dev.carecloud.local']//div[#'businesses']/div/ul[2]/li")[0]; //need to get the 1st element in the returned list, you don't want the whole thing.
foreach(ATag parentNode in Host.Local.Find<ATag>("ul/li/div/div[2]/p/a")) {
// Print parent's name here
foreach(ATag childNode in parentNode.Find<ATag>("ancestor::li/ul/li/div/div[2]/p/a")) {
//Print child's name here
}
}
Shortcuts usually aren't...
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
I was gonna try using GetPath() and truncating that but I'll try this. I'm kinda digging this tool after initially thinking "bah, it's all record and playback" but once you pop the hood, there's some good stuff under there. I just have to get used to it
Re: get item count and item text for an unordered list?
Yeah, there's a lot of power in there if you know how to get to it.
GetPath returns the full, absolute path of the object, so it is useful for certain things, but not for "inner" loops like what you need.
If you use the repository (which I'm assuming you don't since you are heavy in code, but you should really consider it, makes maintenance very easy), then you should also look at using the BasePath (for folders) and Path (for items) attributes. They return just what is in the repository "Path" column, so if you setup your repo the right way you can use them to get the paths you need and only need to update the repo object paths instead of your code.
GetPath returns the full, absolute path of the object, so it is useful for certain things, but not for "inner" loops like what you need.
If you use the repository (which I'm assuming you don't since you are heavy in code, but you should really consider it, makes maintenance very easy), then you should also look at using the BasePath (for folders) and Path (for items) attributes. They return just what is in the repository "Path" column, so if you setup your repo the right way you can use them to get the paths you need and only need to update the repo object paths instead of your code.
Shortcuts usually aren't...
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
I'm learning the tool coming from SilkTest and Selenium/Java so I'm using the repository but also experimenting with straight coding. Not everyone on the team is comfortable writing code so we'll probably mix and match as needed.
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
BTW, I tried using the latest code snippet and get this
Cannot implicitly convert type 'Ranorex.ATag' to 'Ranorex.LiTag' (CS0029)
Cannot implicitly convert type 'Ranorex.ATag' to 'Ranorex.LiTag' (CS0029)
Re: get item count and item text for an unordered list?
Hmm...
Maybe I missed something in my example. Which line is returning the error, specifically?
Maybe I missed something in my example. Which line is returning the error, specifically?
Shortcuts usually aren't...
-
- Posts: 13
- Joined: Wed Nov 20, 2013 4:16 pm
Re: get item count and item text for an unordered list?
LiTag root = ......
Re: get item count and item text for an unordered list?
Yeah, my bad... Duh...
The root declaration should have been "...Find<LiTag>(..." instead of "...Find<ATag>(...".
The "<T>" (where T is a class type, like LiTag) is a generic declaration, and tells .NET to make sure what it finds is of type "T". I had changed the RXPath, but not the <T>.
The root declaration should have been "...Find<LiTag>(..." instead of "...Find<ATag>(...".
The "<T>" (where T is a class type, like LiTag) is a generic declaration, and tells .NET to make sure what it finds is of type "T". I had changed the RXPath, but not the <T>.
Shortcuts usually aren't...