Dynamic coding using C# - Find children

Ask general questions here.
vivek
Posts: 5
Joined: Wed Dec 03, 2014 7:32 pm

Dynamic coding using C# - Find children

Post by vivek » Thu Feb 12, 2015 4:20 am

Hi Team

I need to select the check boxes and radio buttons in my screen. Please refer the below code snippet.It works fine and I am selecting the checkboxes 1 after the other and also I am hardcoding in to less then or equal to 3

Code snippet 1:
******************

DivTag DTtag = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//div[@id~'.*QuestionSetLV-body']");
for(int i=1;i<=3;i++)
{
ImgTag img = DTtag.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
img.Click();
}

Here, I have 2 questions.

1) I have tried the same by using "FindChildrens", however i not successful. It's always gives the count as 0. Attached code snippet 2 for your reference.
2) Is there any way to select all the checkboxes at 1 shot?

Please advise

Code snippet 2:
*****************
DivTag DTtag = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//div[@id~'.*QuestionSetLV-body']");
ImgTag img = DTtag.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
IList<ImgTag> ImgChild = img.FindChildren<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
foreach (ImgTag obj in ImgChild)
{
obj.Click();
}
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Dynamic coding using C# - Find children

Post by Support Team » Thu Feb 12, 2015 1:37 pm

vivek wrote:I have tried the same by using "FindChildrens", however i not successful
Attention, the FindChildren(string defaultLabel) does not take a RanoreXPath as argument, but a label. As this already caused confusion, the method is marked as obsolete (you get a warning if using it) and will be deleted in a future version of Ranorex.

IMHO what you are searching for is the Find(RxPath) method which returns you all elements satisfying the passed RanoreXPath.
vivek wrote:Is there any way to select all the checkboxes at 1 shot?
Just use the Find instead of the FindChildren method to retrieve all ImgTag elements and loop through the returned elements.

Regards,
Alex
Ranorex Team

vivek
Posts: 5
Joined: Wed Dec 03, 2014 7:32 pm

Re: Dynamic coding using C# - Find children

Post by vivek » Fri Feb 13, 2015 4:08 am

Hi Alex

Thanks for your help. I have coded using "Find" and it works.

Please refer the code snippet. It works this way. Here I am finding my object once again inside the loop and click it

Code snippet Worked

ImgTag img = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
IList<ImgTag> ImgChild = img.Find<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']"
foreach (ImgTag obj in ImgChild)
{
ImgTag imgclk = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
imgclk.Click();
}

On the flip side, See below. I am referencing the for each loop object and clicked it. I am not successful here, It find the first image object but it't not able to find the other object.

I am getting error message as "Could not get a valid element rectangle image". I have checked in spy and it can able to recognize all the images.

Any help will be appreciated

Code snippet - having issues
********************************

ImgTag img = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
IList<ImgTag> ImgChild = img.Find<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']"
foreach (ImgTag obj in ImgChild)
{
obj.Click();
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Dynamic coding using C# - Find children

Post by Support Team » Mon Feb 16, 2015 12:13 pm

Hi vivek,

It is most probably a problem with caching. You are searching for all img elements and save this elements in your list, and it seems that your web page is reloaded during the click actions Ranorex performs. This is why Ranorex can no longer get a valid rectangle, since the img element saved does no longer exist, a new one was created instead.
May I ask you to post a Ranorex snapshot file of your img elements here? This will help me to to help you writing a suitable method.
The following link will show you how to generate a snapshot file: Creating Ranorex Snapshot Files.

Why Ranorex always clicks on the first element in the first method is because you are always searching for the same element, with the same RxPath. When there are more elements with the same RxPath Ranorex will just return the first one.

Regards,
Markus