Page 1 of 2

How to select a Option in a Select tag element (Dropdown)

Posted: Fri Sep 04, 2009 8:27 am
by vinoappu
Can anyone please help me out - How to select a Option in a Select tag element (Dropdown) ??

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Fri Sep 04, 2009 6:17 pm
by Ciege
First find the combobox

Code: Select all

DOMCombo = webDocumentName.FindSingle(".//select[@Id='" + ComboName + "']");
Next you *can* check if the option exists before trying to click it (this is optional).

Code: Select all

foreach (OptionTag option in DOMCombo.Find(".//option"))
{
if (option.InnerText == ComboItem)
{
ComboItemFound = true;
break;
}
}
Finally click the option.

Code: Select all

DOMCombo.EnsureVisible();
DOMCombo.Focus();
DOMCombo.Click(Location.CenterRight, 1000);
ListItem item = "/container[@caption='selectbox']/listitem[@text='" + ComboItem + "']";
item.Click(Location.Center, 1000);

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Wed May 19, 2010 3:55 pm
by sdaly
Hi Ceige, your repsonse helped me, thank you! I was previously trying to find and click the optiontag and going nuts wondering why it wasn't working! I now have the below which works a treat!

Public Sub dropDown(dropDownPath As String, itemText As String)
'find and click on dropdown as per specified path
Dim typeSelect As Ranorex.SelectTag = dropDownPath
typeSelect.EnsureVisible
typeSelect.Click(location.CenterRight)
Dim item as Ranorex.ListItem = "/container[@caption='selectbox']/list/listitem[@accessiblename='" & itemText & "']"
item.Click(location.Center)
End Sub

Thanks
Scott

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Wed May 19, 2010 3:59 pm
by Ciege
Great, glad I could be of some help!

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Thu May 27, 2010 9:46 am
by hobli
Hi,

I tried this solution, and it seems only work with IE. When I try with FireFox (3.6.3), Ranorex 2.3.1 can only recognise "container[@caption='dropdown']" (using Ranorex Spy), all the list items inside this container can not be found. I have tried with code to search all child elements under this container in real-time, but still fails to find any child elements.

Is there any way Ranorex can find the relevant item in the dropdown list with FireFox?

thanks a lot

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Thu May 27, 2010 10:50 am
by Support Team
Hi,
hobli wrote:Is there any way Ranorex can find the relevant item in the dropdown list with FireFox?
I think the drop down items don't exist at the moment you try to get them.
So please try to open the drop down before you call your method to get all list items. So it's guaranteed that all items exists at runtime.

Is the drop down an element of a web page or you try to automate a Firefox application drop down?

Regards,
Peter
Ranorex Support Team

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Fri May 28, 2010 11:18 am
by hobli
Hi,

I think the dropdown list is available before I do the option/item selection with the code, the same code works with IE.

To ensure the drop down list is available, I use the "ctrl+win" short-cut key of Ranorex Spy to retrieve the item list.

If there's a solution with FireFox, can you pls post a piece of code to give me a hint?

thanks a lot

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Fri May 28, 2010 12:00 pm
by Support Team
Hi,

I tried following code in both browsers and it work as expected
Ranorex.SelectTag selectTag = Host.Local.FindSingle("/dom[@page='test.html']/body/select");            	
foreach(Ranorex.OptionTag optTag in selectTag.Find(".//option"))
{
     Console.WriteLine(optTag.InnerText);
}
hobli wrote:Ranorex 2.3.1 can only recognise "container[@caption='dropdown']" (using Ranorex Spy)
You are using the DOM object to get the elements of the web page or you using the MSAA drop down of the Form of Firefox?
//DOM dorp down
"/dom[@page='test.html' and @path='C:/Users/pgrad/Desktop/test.html' and @browsername='Mozilla']/body/select"
//Form drop down
"/form[@processname='firefox']/element/container[1]/element[1]/container/combobox[@accessiblevalue='Test 1']"
Regards,
Peter
Ranorex Support Team

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Mon May 31, 2010 10:17 am
by hobli
Hi, Ranorex support team,

Think there's mis-communication.
What I am trying to do is as following:

1. find the location of the "selectTag"/combobox in the browser (FF)
2. do a mouse click on the combobox
3. search the "drop-down window", RxPath = "/container[@caption='dropdown']" (path found using ranorexSpy)
assumption: as illustrate in this post earlier, similar path is expected to locate dropdown window in FF as in IE
4. try to find the relevant option item/list item insider the container
Result: "container" found, but there are 0 elements under "containter".
5. do a mouse click over the option item in the dropdown window

If I repeat the same step (use rxpath of container: "/container[@caption='selectbox']") for IE, it works.

I am using DOM to allocate the location of the combobox first, then use MSAA to locate the container and its option item.
Btw, I can use this rxpath to locate comboBox in FF, however:
"/form[@processname='firefox']/element/container[1]/element[1]/container/combobox[@accessiblevalue='Test 1']"

thanks

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Mon May 31, 2010 3:58 pm
by Support Team
Hi,
hobli wrote:Btw, I can use this rxpath to locate comboBox in FF, however:
"/form[@processname='firefox']/element/container[1]/element[1]/container/combobox[@accessiblevalue='Test 1']"
Sorry but in Firefox you don't get the items of a combo box, because Firefox MSAA implementation is not designed as has been imagined. Therefore, it is not currently possible to implement this. Then only way is to use the DOM "select and option" feature.

Regards,
Peter
Ranorex Support Team

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Tue Aug 02, 2011 5:04 pm
by mrusso
I recently asked about a similar problem and have a suggestion based on code that worked for me.

simple example:
OptionTag option = "//rxpath/to/option";
option.Selected = true;
example of an extension method adding this capability to all SelectTag elements:
using System.Text.RegularExpressions;

public static void SelectOption(this SelectTag selectBox, string itemName)
{
	Report.Info("Selecting item '"+itemName+"' in "+selectBox);
	
	// ignore case regex
	string itemRegex = "^(?i)" + Regex.Escape(itemName) + "$";

	selectBox.EnsureVisible();
			
	OptionTag option = selectBox.FindSingle<OptionTag>("option[@InnerText~'"+itemRegex+"']");
	option.Selected = true;
}

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Fri Mar 08, 2013 9:57 pm
by omayer
Hi All,
How to verify that the list item got selected and if NOT then retry couple of more times and then exit, Thank you in Advance

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Fri Mar 08, 2013 10:19 pm
by Ciege
Put your select in a loop, after the select, find the combo and read it's selected item. If it is correct, exit the loop. If it is not correct loop back to the top of the loop. Exit the loop after X tries...

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Sun Mar 10, 2013 11:43 pm
by omayer
Thank you Ciege, it worked

Code: Select all

public static void ClickOnSelectTagWithFireEvent(Ranorex.SelectTag  selectTag, string comboItem)
			{
	 	   	
		 	   	int attemptsToSelect =0;
				int maxAttemps = 3;
	 	   		try{ 	 	   							
					while(attemptsToSelect != maxAttemps)
						{
							selectTag.EnsureVisible();
							selectTag.Focus();
							selectTag.Click(Location.CenterRight, 1000);
							ListItem item = "/container[@caption='selectbox']/listitem[@text='" + comboItem + "']";
							item.Click(Location.Center, 1000);
							Thread.Sleep(5000);
							//break;
							if(item.Text == comboItem)
							{
								break;
							}
						}  
	
	 	   			} //Try close
	 	   		
	 	   		catch {
				 //Force Test Steps to FAILED- if error then exit from the test case
			 	 Report.Error("Unable to find Select Item: " + comboItem);
			     throw new Ranorex.ElementNotFoundException("SelectItem Not Found:"+comboItem, null); 
			    }
			}

Re: How to select a Option in a Select tag element (Dropdown)

Posted: Mon Mar 11, 2013 4:16 pm
by Ciege
Glad it works for you!

However, I don't see where you are incrementing your attemptsToSelect variable. Without incrementing that var this method could loop forever.