How to Select all options in a Select tag drop down

Class library usage, coding and language questions.
houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

How to Select all options in a Select tag drop down

Post by houseofcutler » Thu May 01, 2014 1:10 pm

Hi all,

Spent a while on this and so far I am not winning. Hopefully someone can help me.

I need to be able to select each option within a specified drop down list. Because of the way this page is built I must click on each option or the page will not refresh and trigger other things.

I have looked at a previous thread - http://www.ranorex.com/forum/how-to-sel ... -t998.html - This got me this far:
public void Select_Each_Option3()
        {
        Ranorex.SelectTag varSelect = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;
                
        foreach (OptionTag option in varSelect.Find(".//option"))
        	{
	        var varListItem = option.InnerText;
	            
	        varSelect.EnsureVisible();
			varSelect.Focus();
			varSelect.Click(Location.CenterRight, 1000);
							
			
			ListItem item = "/container[@caption='selectbox']/listitem[@text~'" + varListItem + "']";
			item.EnsureVisible();
			item.Focus();
			item.Click(Location.Center, 1000);
			Report.Info("Selecting Item '"+varListItem+"' in "+varSelect);
           	
        	}
        }
This will select a couple of options before failing while not being able to find an element.
I am also getting the following message in the log "Could not get a valid element rectangle from '{ListItem:None}', since the element is no longer valid. "

I guess what is happening is that the listitem list dosen't exist anyone once the selectbox is closed. I have reached the limiot of my c# knowledge and cant think of a way around the issue.

Can anyone offer a solution please?

Many thanks

Ben

KurtZ
Posts: 12
Joined: Fri Jul 12, 2013 2:24 pm

Re: How to Select all options in a Select tag drop down

Post by KurtZ » Thu May 01, 2014 4:36 pm

Move expanding the list outside the loop. You are expanding, selecting an option, restarting the loop, then collapsing and trying to select the next option from a closed list.
{  
        Ranorex.SelectTag varSelect = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;  
         varSelect.Click(Location.CenterRight, 1000);  
                  
        foreach (OptionTag option in varSelect.Find(".//option"))  
            {  
            var varListItem = ".//listitem[@text~'" + varListItem + "']";  
            varListItem .Click(Location.Center, 1000);  
            Report.Info("Selecting Item '"+varListItem+"' in "+varSelect);  
              
            }  
        }

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: How to Select all options in a Select tag drop down

Post by houseofcutler » Fri May 02, 2014 11:23 am

Thank you very much for your reply

I thought the action to click on the list needed to be within the loop as once I click on an option it closes the list which then has to be reopened to in order to see the option I want to click next.


I tried your script but got an error "Cannot use local variable 'varListItem' before it is declared (CS0841)"
I also had to change the variable type for var item to ListItem as var (sting) was not enabled for Click().

I had a go at fixing things but I still haven't been able to come up with something that works. It will select teh first item in the list then fails as it cant find others - No element found for path '/container[@caption='selectbox']/listitem[@text~'']'


Here is how the code looks currently
public void Select_Each_Option5()
        {    
        Ranorex.SelectTag varSelect = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;    
         varSelect.Click(Location.CenterRight, 1000);    
                    
        foreach (OptionTag option in varSelect.Find(".//option"))    
            {    
            var varListItem = option.InnerText;
        	ListItem varItem = "/container[@caption='selectbox']/listitem[@text~'" + varListItem + "']";
            varItem.Click(Location.Center, 1000);    
            Report.Info("Selecting Item '"+varListItem+"' in "+varSelect);    
                
            }    
        }
This 'simple' task really isn't so simple it seems. If you (or anyone else) have any other thoughts please share them.

I am very regretful for the help.

KurtZ
Posts: 12
Joined: Fri Jul 12, 2013 2:24 pm

Re: How to Select all options in a Select tag drop down

Post by KurtZ » Fri May 02, 2014 1:24 pm

D'oh! I didn't think about the list collapsing after you selected an item. You're original code was right then. Does your list have a scroll bar? Does it only select the visible list items before failing?
Last edited by KurtZ on Fri May 02, 2014 1:31 pm, edited 1 time in total.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to Select all options in a Select tag drop down

Post by krstcs » Fri May 02, 2014 1:24 pm

If you are using the object repository, have you tried turning off object caching for the select tag object? It looks like the cache is being invalidated at some point.

Also, this could be a timing issue. Have you tried adding a delay of a second or so in between the selections?

Also, if the item isn't being selected due to scroll issues, have you tried just using the keyboard to select the option? Use up/down and enter. I have found it to be much more reliable than the mouse on select/option issues.
Shortcuts usually aren't...

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: How to Select all options in a Select tag drop down

Post by houseofcutler » Thu May 08, 2014 11:50 am

Hi KurtZ

sorry for the delayed reply - I have looked at this a couple of times since your last update - thanks for that by the way.

There is no caching enabled for these objects. I have also tried adding delays using my original code but it do sent work.

I think I am going to opt for a work around - I can Click on the drop down and use the arrow keys and enter to select something - This isn't a fool proof way as depending on which item is already selected to start with I may already be at the bottom of the list and therefore pressing 'down' does not select another record.

It will have to do for now anyway - I may revisit this in the future.

Thanks for your help. 8)

KurtZ
Posts: 12
Joined: Fri Jul 12, 2013 2:24 pm

Re: How to Select all options in a Select tag drop down

Post by KurtZ » Thu May 08, 2014 1:52 pm

That was krstcs with the arrow key solution by the way :wink:

I am looking at your original solution, and it seems really convoluted. First off, I don't think you want to invoke the find inside the loop; essentially you're saying "for each option in the collection I find, find a collection of options". Second, once you create the collection, you already have the options you want to select, there is no need to declare a corresponding list item and click that.
public void Select_Each_Option3()
		{
			Ranorex.SelectTag SendToDDL = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;
            // Create a collection of all options within the DDL, then loop
            OptionTag OptionsToSelect = SendToDDL.Find(".//option")
			foreach (OptionTag SingleOption in OptionsToSelect)
			{
				SingleOption.MoveTo() // Sometimes a MoveTo also works
				SingleOption.Click(Location.Center, 1000);
			}
		}
I have a similar issue, so instead of clicking an item, I just set an attribute on the field itself using my option. Maybe something like this would help?
public void Select_Each_Option3()
		{
			Ranorex.SelectTag SendToDDL = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;
            // Create a collection of all options within the DDL, then loop
            OptionTag OptionsToSelect = SendToDDL.Find(".//option") 
			foreach (OptionTag SingleOption in OptionsToSelect)
			{
				// Set the field's TagValue with each option's TagValue				
				SendToDDL.Element.SetAttributeValue("TagValue",SingleOption.TagValue);
			}
		}

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: How to Select all options in a Select tag drop down

Post by houseofcutler » Fri May 09, 2014 4:51 pm

Hi KurtZ,

I admire your tenacity - Thank you for another suggestion. Running your scripts I get an error "Cannot implicitly convert type 'System.Collections.Generic.IList<Ranorex.Core.Element>' to 'Ranorex.OptionTag'.

However what you said gave me some ideas so I updated my original code but left the 'find' off the for each as this seemed to avoid the conversion error.

I added some reporting for debugging purposes and could see that all the options were being selected but could not be clicked on as they were not visible - Back to the menu closing issue again.

I added in a mouse click on the drop down and mouse clicks for the options (just setting the value does not cause the webpage to update) and despite some shaking of the web page this is working now!
public void Select_Each_Option()
        {
        Ranorex.SelectTag sendtoDDL = repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo;
        Report.Info("INFO", sendtoDDL.ToString());
        
        foreach (OptionTag SingleOption in sendtoDDL.Find(".//option"))
        	{
        		repo.GroupcallMessengerRoot.SendMessage.Recipients.SendTo.dd_SendTo.Click();
        		Report.Info("INFO", SingleOption.ToString());
        		SingleOption.MoveTo(); // Sometimes a MoveTo also works
                SingleOption.Click(Location.Center, 1000);
        	}
        }
Thank you for your help and also krstcs sorry for the mix up of the names guys but they are quite similar for those not paying close attention. :oops:

I appreciate that my original solution was pretty convoluted - I have only been learning C# as long as Ranorex so about 5 weeks, I am surviving purely off the forum, Google and a lot of trial and error at the moment 8)

Thanks again