18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to select value from the combo box / option list?

aspatel80aspatel80 Members
edited November -1 in Sahi - Open Source
Hello Team,

I have two combo box / drop down list in my application and I have written following code to select the values in combo box / drop down list.

_setSelected(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$ddlPartyType"), "Non Legal Party");
_setSelected(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$ddlRelationshipType"), "Sample Relationship Type");

Here I have mentioned hard coded values in _setSelected() method. Those are "Non Legal Party" and "Sample Relationship Type".

How can I select any value from the list based on the ID of the option rather than passing the hard coded value?

Suppose my list name is List1 whih contains folloing option:

1. Blue
2. Yellow
3. Red

Now if i want to select the option "Yellow" then I have to write following code:

_setSelected(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$List1"), "Yellow");

But how can I select the same option using the List ID means is it possible to seelct the option in the list based on its displayed order like:

_setSelected(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$List1"), 2); ----> Here I am pasing '2' as the ID of the "Yellow" option is '2' in the list?

Is it posible?

Is it Possible to select the option from th option list dynamically rather than passing the hard coded value?

Thanks,

Ashvin Patel

Comments

  • Hi,

    See if this works:
    _setSelected(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$List1"), _getText(_select("ctl00\$ContentPlaceHolder1\$uMatterParty\$List1").options[1]));
    will select option with index 1.

    Regards,
    Pankaj.
  • narayannarayan Administrators
    Ashvin,

    The current implementation of _setSelected is too basic. I have modified the code for _setSelected to handle identification of options using their ids, or their index. Open sahi/htdocs/spr/concat.js, search for "_setSelected", and replace that function with this code:
    Sahi.prototype._setSelected = function (el, val, isMultiple) {
        var l = el.options.length;
        var optionEl = null;
        if (typeof val == "string"){
            for (var i = 0; i < l; i++) {
                if (this.areEqual(el.options[i], "text", val) ||
                    this.areEqual(el.options[i], "id", val)) {
                    optionEl = el.options[i];
                }
            }
        } else if (typeof val == "number" && el.options.length > val){
            optionEl = el.options[val];
        }
        if (!optionEl) throw new Error("Option not found: " + val);
    
        for (var i = 0; i < l; i++) {
            if (!isMultiple) el.options[i].selected = false;
        }
    
        optionEl.selected = true;
        this.simulateEvent(el, "change");
    }
    
    If you are not sure, let me know, I can email you a build.

    Regards,
    Narayan
  • Will the improvement to _setSelected be included in the next release of Sahi
  • narayannarayan Administrators
    Yes Donna, it will be part of the next build. If you need it urgently, you can pick a nightly build from here: https://sourceforge.net/project/showfiles.php?group_id=151639&package_id=309861
  • Hello Narayan,

    The solution you have provided is perfectly working.

    Thanks for the code change.

    Regards,

    Ashvin
  • Hi, I want to know how to select more values in the same action, i tried to load more values but only take one value in the script and then when i try to run a test case for create one register with that values selected (all, are necessary) only took one. Please I need your help. Thanks
Sign In or Register to comment.