Selecting Option in a Select List (Drop Down List)

Ask general questions here.
Pixi6s
Posts: 92
Joined: Tue Jun 28, 2011 8:57 pm

Selecting Option in a Select List (Drop Down List)

Post by Pixi6s » Thu Oct 06, 2011 3:35 pm

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?
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

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

Post by Support Team » Fri Oct 07, 2011 11:25 am

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

Pixi6s
Posts: 92
Joined: Tue Jun 28, 2011 8:57 pm

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

Post by Pixi6s » Fri Oct 07, 2011 9:42 pm

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.

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

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

Post by Ciege » Fri Oct 07, 2011 11:10 pm

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

natasha
Posts: 5
Joined: Mon Oct 17, 2011 7:18 pm

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

Post by natasha » Wed Nov 23, 2011 12:37 am

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

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

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

Post by sham526 » Wed Nov 23, 2011 10:22 am

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

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

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

Post by omayer » Wed Jun 12, 2013 5:01 pm

How to select 1st item from the list box since items are dynamically changed.
Thank you,
Tipu

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

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

Post by Support Team » Thu Jun 13, 2013 3:38 pm

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)