Find all instances of a button

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Find all instances of a button

Post by regex » Tue Nov 13, 2012 6:19 pm

For some reason I'm having a problem with this. I've gone for about 2 months with iterating through buttons on pages and finding everything I need. So forgive me for asking something here. But I'm trying to click this export button from every instance within a page.

I try to use:

Code: Select all


ranorex.spantag tag; 
ArrayList btnArr = new ArrayList();
btnArr.add(tag); 

foreach(string button in btnArr)
{
mouse.moveTo(button);
mouse.click(button); 
}

But it doesn't find every instnance of that button within the page. I tried adding wild cards for changing paths but host.local.tryfind single won't compile the xpath derivatives.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Find all instances of a button

Post by Ciege » Tue Nov 13, 2012 6:42 pm

Doesn't look like your code will do much of anything actually... See my comments in your code.

Code: Select all

ranorex.spantag tag;   //Creates a spantag element named tag
ArrayList btnArr = new ArrayList();  //Creates an Array
btnArr.add(tag);   //Adds the tag element to the array (although the tag element is empty since you didn't assign anything to it)

foreach(string button in btnArr)  //Foreach through the array of item (the empty tag element you added)
{
mouse.moveTo(button);
mouse.click(button); 
}

So, to get each spantag element on the screen you need to search for them all and put them all into an iList.

Code: Select all

//First make an iList of all spantags on your screen. NOTE: You need to change the word MYROOT below to the element you want to search from.
IList<Ranorex.spantag> MySpanTags = MYROOT.FindChildren<Ranorex.spantag>();

//Next, foreach through all the spantags
foreach (Ranorex.spantag MySpanTag in MySpanTags)
{
  //Do what you want here.
}
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Tue Nov 13, 2012 8:31 pm

That's not working for some reason. I reverted to this. How do you build the absolute path from within here:

Code: Select all

string mid = "path"; 
string here; 
Ranorex.SpanTag btnSp;
Duration timeout = 30000;

while(Host.Local.TryFindSingle(mid, timeout, out btnSp))
       		{
       			btnSp.GetPath().ToString();
       			here = btnSp.ToString();
       			
       			list.Add(btnSp);
       			Ranorex.Mouse.MoveTo(btnSp); 
       			//btnSp.Click();
       		}


The get path isn't workign to build the absolute path!

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Find all instances of a button

Post by Ciege » Tue Nov 13, 2012 9:49 pm

That's not working for some reason.
What's not working? Can you elaborate?
It should work fine so long as 1, you start with a valid root object and 2, looking for spantags is actually what you want to do.
How do you build the absolute path from within here
Why? You don't need to build a path. If you make an iList of elements on your page you can just call the click method for each element.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Tue Nov 13, 2012 9:58 pm

1)

The IList is not working. There is javascript preventing full recursion through tags.

2)

How do I use the GetPath().ToString() to build the full path for each element and THAN find the next one from within the host.local while loop I provided.

Thanks.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Find all instances of a button

Post by Ciege » Tue Nov 13, 2012 10:10 pm

Well, GetPath gets the path of an element, which would mean you would already need to know the element. If you already have the element, you don't need to get it's string path representation to click it you just call the click method for the element...

In your code you are not setting the variable mid to anything... It seems like to me you are trying to code the path to the button in the mid string? If that is the case you will need to either set mid to a repo element, or get the full path from RanorexSpy...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Tue Nov 13, 2012 10:33 pm

It is set within my code. I just deleted the path when posting.

I am trying to click on all buttons that say "export" on the page.

That's really all I'm trying to do here. Is there anyway to set a simple string and search throughout the page for that and click?

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Find all instances of a button

Post by Ciege » Tue Nov 13, 2012 10:38 pm

Is there anyway to set a simple string and search throughout the page for that and click?
That would be with the findchildren (that I posted above) or find descendants (same as findchildren but goes deeper).

I'm not sure what you mean by
The IList is not working. There is javascript preventing full recursion through tags.
... I don't understand why javascript has anything to do with finding all the elements you want to find... That is the way that works to find all the elements that are a child or descendant to a root object.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Wed Nov 14, 2012 7:02 pm

I finally got a sufficient way of doing this. I did use the finddescendents and the Ilist. It took some time to get the code together.

However I want to get the full RXpath when building certain elements. I am calling repository items and doing .GetPath().ToString() but I'm only getting relative paths. How do I get the full path with this code?

Code: Select all

Host.Local.TryFindSingle(portalqaRepository.Instance.Path.GetPath().ToString(), timeout, out openButton);

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

Re: Find all instances of a button

Post by Support Team » Thu Nov 15, 2012 9:13 am

regex wrote:I finally got a sufficient way of doing this. I did use the finddescendents and the Ilist. It took some time to get the code together.
You can easily get a list of all span tags using an RanoreXPath and the Find method:
WebDocument dom = ...; // get the DOM here
var allSpanTags = dom.Find<SpanTag>(".//span");
regex wrote:However I want to get the full RXpath when building certain elements
To get the absolute path of a repo item, use the "Info" object for that item:
RxPath absolutePath = repo.Instance.AppFolder.ItemInfo.AbsolutePath;
Regards,
Alex
Ranorex Team

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Thu Nov 15, 2012 8:43 pm

Thank you very much for this information Alex. It has certainly helped create absolute path items. I am grabbing inner text from various rows and counting them. The form changes based on month and needs to handle dynamic changes.

The report has several TR tags. I need to add the inner text together from tr[#'abc_1']/td[6]/div[2] + tr[#'abc_2']/td[6]/div[2] and so on for any variation.

Code: Select all

TrTag allspan = "/dom[@domain='abc']//tr[@id~'abc_[0-9]']";
The abc_1 path containes a Trtag that changes from abc_0 to abc_1 for each additional row in the report. So theoretically it could go to abc_20000. I put an expression like tr[@id~'abc_[0-9]'] in the tag but it doesn't find the items.

Code: Select all

IList<TdTag> inners = allspan.FindDescendants<TdTag>("/td[6]/div[2]");
The /td[6]/div[2] is the tag I need but it does not grab when the code is run.

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

Re: Find all instances of a button

Post by Support Team » Fri Nov 16, 2012 12:59 pm

Hi,

You could use these RxPaths:
TrTag allspan = "/dom[@domain='abc']//tr[@id~'^abc.*']";
//OR just:
TrTag allspan = "/dom[@domain='abc']//tr[@id~'abc']";
if you want to search for all TdTags which are children of the div tag you have to use the following code:
IList<TdTag> inners = allspan.Find<TdTag>(".//td[6]/div[2]/td");
//OR:
IList<TdTag> inners = yourDivTag.FindDescendants<TdTag>();
for more information about how the find methods work please take a look at out online API: Adapter Class.

Regards,
Markus

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Find all instances of a button

Post by regex » Fri Nov 16, 2012 5:43 pm

First - Thank you for taking time to answer these precisely. Each iteration of questions gets me closer to the solution.

This code works well.

Code: Select all

IList<DivTag> inners = allspan2.Find<DivTag>(".//td[6]/div[2]");


However it's only returning the first instance of the this tag:

Code: Select all

 Ranorex.TrTag allspan2 = "/dom[@domain='devvprtweb405']//tr[@id~'^ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__.*']";
There are three TR tags it should be iterating through:

Code: Select all

ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__0
ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__1
ctl00_ctl00_Content_ContentPlaceHolderMain_ReportGrid_ctl00_ctl04_ctl00_PrelimByMonthReportGrid_ctl00__2
It's only returning the first possible value for the TR tag which is the PrelimByMonthReportGrid_ctl00__0 tag. The issue is obviously with the allspan2 field. I'm very close now. I did however write code last night to do a tree search and found the solution. However that required manually concatinating DOM elements versus this method of easily traversing the DOM with your API.

When I put this into the repository:

.//tr[@id~'(?i:PrelimByMonthReportGrid_ctl00)']/td[6]/div[2]

And Right click on properties and change the axis to find all descendents. It highlights all the needed elements. Otherwise this other solution isn't working.
Last edited by regex on Fri Nov 16, 2012 5:59 pm, edited 1 time in total.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Find all instances of a button

Post by Ciege » Fri Nov 16, 2012 5:51 pm

Try changing your .Find to .FindChildren or .FindDescendants as I mentioned above.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...