Page 1 of 1

Selecting Option in a Select List (Drop Down List)

Posted: Thu Oct 06, 2011 3:35 pm
by Pixi6s
I am using Ranorex 3.1 and C#. I am using IE 9.

The web site I am testing has a Drop Down (Select List) and I need to change the value of this during my test runs. I really feel like I am missing something because this has not been easy. I don't care if I accomplish this via the record module or user code. When I do record it clicks the asset that is behind the list when it is "dropped down". I just want a "set" option available for this asset. It's very frustrating.

Any ideas?

Re: Selecting Option in a Select List (Drop Down List)

Posted: Fri Oct 07, 2011 11:25 am
by Support Team
Hi,

To select a specific option tag in your select tag you could use following code
SelectTag someSelectTag = "/dom[@domain='www.w3schools.com']/body/table[2]/tbody/tr[1]/td[2]/iframe/body/select";
OptionTag optTag = someSelectTag.FindSingle(".//option[@value='Volvo']");
optTag.Selected = true;
The sample page is http://www.w3schools.com/tags/tryit.asp ... tml_option
The code above works in both browsers (IE and Firefox).

Regards,
Peter
Ranorex Team

Re: Selecting Option in a Select List (Drop Down List)

Posted: Fri Oct 07, 2011 9:42 pm
by Pixi6s
That did work, I see the select box change values, unfortunately it didn't fire off the code that is suppose to be called when the drop down is changed. If you have any other suggestions let me know, otherwise I will just have to use the first letters and Up and Down Arrow to navigate it.

Re: Selecting Option in a Select List (Drop Down List)

Posted: Fri Oct 07, 2011 11:10 pm
by Ciege
Try this method:

Code: Select all

        public static int DOMComboSelect(Ranorex.WebDocument webDocumentName, string ComboName, string ComboItem)
        {
            /************************************************************************
             * Function         : DOMComboSelect(Ranorex.WebDocument webDocumentName, string ComboName, string ComboItem)
             *
             * Description      : This method will select a ComboBox item in a web document. 
             *                         
             * Parameters       : webDocumentName   - The web DOM object the the ComboBox lives on
             *                  : ComboName         - Name of the ComboBox to click
             *                  : ComboItem         - Item in the ComboBox to select.
             *                  
             * Return Type      : Integer
             * 
             * Return Value     : 0 for success, -1 for failure
             * 
             * Revision History
             * Date       : Author                    : Reason/Change
             * ---------- : ------------------------- : ------------------------------
             * 03/10/2009 : Chris Gendreau            : Initial Creation 
             ************************************************************************/

            Ranorex.SelectTag DOMCombo;

            //Search for the ComboBox on the page
            try
            {
                webDocumentName.EnsureVisible();
                Thread.Sleep(500);
                DOMCombo = webDocumentName.FindSingle(".//select[@Id='" + ComboName + "']");
            }

            catch (RanorexException e)
            {
                Report.Error("Unable to find ComboBox: " + ComboName);
                Console.WriteLine(e.ToString());
                Report.Screenshot();
                return -1;
            }

            //Verify Item exists in ComboBox
            int intResult = DOMComboVerifyItem(webDocumentName, ComboName, ComboItem);
            if (intResult == -1)
            {
                Report.Error("Item: " + ComboItem + " is not a valid item in the ComboBox: " + ComboName);
                return -1;
            }

            //Click a ComboBox & Item
            try
            {
                Report.Info("Selecting item: " + ComboItem + " in  ComboBox: " + ComboName);
                DOMCombo.EnsureVisible();
                DOMCombo.Focus();
                Thread.Sleep(1000);
                DOMCombo.Click(Location.CenterRight, 1000);
                Thread.Sleep(1000);
                ListItem item = "/container[@caption='selectbox']/listitem[@text='" + ComboItem + "']";
                item.Click(Location.Center, 1000);
                Report.Debug("  Selected item: " + ComboItem + " in  ComboBox: " + ComboName);
            }

            catch (RanorexException e)
            {
                Report.Error(e.ToString());
                Report.Screenshot();
                return -1;
            }

            return 0;
        } //End DOMComboSelect

Re: Selecting Option in a Select List (Drop Down List)

Posted: Wed Nov 23, 2011 12:37 am
by natasha
Hi - I am having the same problem as Pixi6s above... if I use the following code, the code that is supposed to fire on dropdown selection change does not execute:

Code: Select all

ListItem item = "/container[@caption='selectbox']/listitem[@text='" + name + "']";		
OptionTag option = dropDown.FindSingle(".//option[@innertext='"+ name +"']");

option.Selected = true;
item.Click();


If I remove the 'option.Selected' property as detailed below, the dropdown change action executes properly, but if the list item is out of view and requires scrolling, the wrong ListItem is selected.

Code: Select all

ListItem item = "/container[@caption='selectbox']/listitem[@text='" + name + "']";	

OptionTag option = dropDown.FindSingle(".//option[@innertext='"+ name +"']");

//option.Selected = true;
item.Click();


Hope you can help with this.

Thanks,
Natasha

Re: Selecting Option in a Select List (Drop Down List)

Posted: Wed Nov 23, 2011 10:22 am
by sham526
you can use the below function which we created for our purpose.

Find_str is the string which need to be found and office_path is the object for the selectTag.


Public Function fnDropDown_FindOption(ByVal Find_str As String, ByVal office_path As Ranorex.SelectTag) As Boolean

fnDropDown_FindOption = False
Try
Dim optag_office As System.Collections.Generic.IList(Of OptionTag) = office_path.Find(Of Ranorex.OptionTag)("./option")

For c As Integer = 1 To optag_office.Count

Dim opt_office_rxpath As New RxPath("./option[" + c.ToString + "]")
Dim office_opt_port As Ranorex.OptionTag = office_path.FindSingle(Of Ranorex.OptionTag)(opt_office_rxpath)
Dim offname As String = office_opt_port.InnerText.Trim

If offname.ToLower = Find_str.ToLower.Trim Then
office_path.EnsureVisible()
office_path.Click()
optag_office(c - 1).Selected = True
optag_office(c - 1).PerformClick()
office_path.Click()
fnDropDown_FindOption=True
Exit For
End If
Next

Catch ex As RanorexException
MsgBox(ex.ToString)
End Try
End Function


This Function will return True if the required was selected else it will return False

Re: Selecting Option in a Select List (Drop Down List)

Posted: Wed Jun 12, 2013 5:01 pm
by omayer
How to select 1st item from the list box since items are dynamically changed.
Thank you,

Re: Selecting Option in a Select List (Drop Down List)

Posted: Thu Jun 13, 2013 3:38 pm
by Support Team
Hello,

You could use 'first()=true' to get the first item from a list.
Please take a look at our User Guide for more information:
http://www.ranorex.com/support/user-gui ... xpath.html

Regards,
Markus (T)