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

Best practices, code snippets for common functionality, examples, and guidelines.
vinoappu
Posts: 6
Joined: Thu Aug 06, 2009 10:13 am

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

Post by vinoappu » Fri Sep 04, 2009 8:27 am

Can anyone please help me out - How to select a Option in a Select tag element (Dropdown) ??

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

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

Post by Ciege » Fri Sep 04, 2009 6:17 pm

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);
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...

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

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

Post by sdaly » Wed May 19, 2010 3:55 pm

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

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

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

Post by Ciege » Wed May 19, 2010 3:59 pm

Great, glad I could be of some help!
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...

hobli
Posts: 57
Joined: Thu Jul 30, 2009 1:15 am

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

Post by hobli » Thu May 27, 2010 9:46 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

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

Post by Support Team » Thu May 27, 2010 10:50 am

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
.
Image

hobli
Posts: 57
Joined: Thu Jul 30, 2009 1:15 am

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

Post by hobli » Fri May 28, 2010 11:18 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

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

Post by Support Team » Fri May 28, 2010 12:00 pm

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
Attachments
test.zip
My Test Site
(206 Bytes) Downloaded 399 times
.
Image

hobli
Posts: 57
Joined: Thu Jul 30, 2009 1:15 am

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

Post by hobli » Mon May 31, 2010 10:17 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

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

Post by Support Team » Mon May 31, 2010 3:58 pm

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
.
Image

mrusso
Posts: 16
Joined: Thu Feb 03, 2011 12:54 am

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

Post by mrusso » Tue Aug 02, 2011 5:04 pm

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;
}

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

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

Post by omayer » Fri Mar 08, 2013 9:57 pm

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
Tipu

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

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

Post by Ciege » Fri Mar 08, 2013 10:19 pm

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...
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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

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

Post by omayer » Sun Mar 10, 2013 11:43 pm

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); 
			    }
			}
Tipu

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

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

Post by Ciege » Mon Mar 11, 2013 4:16 pm

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.
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...