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

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

Best practices, code snippets for common functionality, examples, and guidelines.

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

Postby vinoappu » Fri Sep 04, 2009 9:27 am

Can anyone please help me out - How to select a Option in a Select tag element (Dropdown) ??
vinoappu
 
Posts: 5
Joined: Thu Aug 06, 2009 11:13 am

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

Postby Ciege » Fri Sep 04, 2009 7: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
Ciege
 
Posts: 908
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

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

Postby sdaly » Wed May 19, 2010 4: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
sdaly
 
Posts: 194
Joined: Mon May 10, 2010 12:04 pm
Location: Dundee, Scotland

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

Postby Ciege » Wed May 19, 2010 4: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...
User avatar
Ciege
 
Posts: 908
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

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

Postby hobli » Thu May 27, 2010 10: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
hobli
 
Posts: 57
Joined: Thu Jul 30, 2009 2:15 am

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

Postby Support Team » Thu May 27, 2010 11: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
User avatar
Support Team
Site Admin
 
Posts: 4297
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

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

Postby hobli » Fri May 28, 2010 12:18 pm

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
hobli
 
Posts: 57
Joined: Thu Jul 30, 2009 2:15 am

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

Postby Support Team » Fri May 28, 2010 1: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 51 times
User avatar
Support Team
Site Admin
 
Posts: 4297
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

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

Postby hobli » Mon May 31, 2010 11: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
hobli
 
Posts: 57
Joined: Thu Jul 30, 2009 2:15 am

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

Postby Support Team » Mon May 31, 2010 4: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
User avatar
Support Team
Site Admin
 
Posts: 4297
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

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

Postby mrusso » Tue Aug 02, 2011 6: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;
}
mrusso
 
Posts: 16
Joined: Thu Feb 03, 2011 1:54 am


Return to How To …

Who is online

Users browsing this forum: No registered users and 0 guests