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.

Need help with Dropdown list: How to pick latest value from dropdown.

srinisrini Members
edited June 2009 in Sahi - Open Source
we have a bunch of dropdowns on the web page. All these dropdown items are having numeric (Ex: 800,809,818 etc...) values and strings. Here i need to count the values from the dropdown and have to pick max value in the dropdown. Hey this is urgent. Please help me out.

Comments

  • Hi,

    Please elaborate with an example.
  • narayannarayan Administrators
    <browser>
    function getMaxOption(selectEl){
       var opts = selectEl.options; // get the options
       var values = []; // blank array
       for (var i=0; i<opts.length; i++){
          var num = parseInt(opts[i].value); // get the integer value of string
          if ("" + num != "NaN")  // check if it is a number and add to array
              values[values.length] = num;
       }
       return values.sort(function(a,b){return a-b;}).pop(); // sort it ascending numerically and return last value.
    }
    </browser>
    
    _alert(getMaxOption(_select("myDropDown"))); // Should alert the max number in this dropdown.
    

    More info on javascript sorting
  • srinisrini Members
    Hi Nayaran,

    We have an internal application LMS in this application we are having 2 pages those are Leave Application and Leave Modification Request. After successful requesting a Leave one number will be generated (Leave Request No) and this will be available in the Leave Modification Request page under Leave Request No dropdown. Here I want to select Leave Request No which was requested earlier means latest Leave Request No from Leave Request No dropdown list.

    Note: we don’t know the latest Leave Request No until unless selecting the dropdown list in the Leave Modification Request page.
  • narayannarayan Administrators
    Srini,

    Did you try the code in my previous reply? I think if you do
    _setSelected(_select("leaveReqNo"),  ""+getMaxOption(_select("leaveReqNo")));
    
    it should work. Change "leaveReqNo" to the identifier of your select box.

    Regards,
    Narayan
  • srinisrini Members
    Hi Narayan,

    i have tried with the following but i am getting null value. i am not getting dropdown list max value.

    function GetMaxValueInDropDown($selectEl)
    {
    var opts = $selectEl.options; // get the options
    var $values = []; // blank array

    for (var $i=0; $i<$opts.length; $i++)
    {
    var $num = parseInt($opts[$i].value); // get the integer value of string

    if ("" + $num != "NaN") // check if it is a number and add to array
    {
    $values[$values.length] = $num;
    }
    }

    return values.sort(function($a, $b){return $a - $b;}).pop(); // sort it ascending numerically
    and return last value.

    _alert($values);

    _setGlobal("LeaveRequestDDMaxValue", $values[0]);
    }
  • srinisrini Members
    Hi sahi_tester

    We have an internal application LMS in this application we are having 2 pages those are Leave Application and Leave Modification Request. After successful requesting a Leave one number will be generated (Leave Request No) and this will be available in the Leave Modification Request page under Leave Request No dropdown. Here I want to pick Leave Request No which was requested earlier means latest Leave Request No from Leave Request No dropdown list.

    Note: we don’t know the latest Leave Request No until unless selecting the dropdown list in the Leave Modification Request page.
  • narayannarayan Administrators
    Srini,

    Are the options in the dropdown visible as numbers when you look at them? If yes, there is a small change needed in the function.

    Btw, you have to use the code as is. No need for modifications. Remember to use the browser tags for that function.

    <browser>
    function getMaxOption(selectEl){
       var opts = selectEl.options; // get the options
       var values = []; // blank array
       for (var i=0; i<opts.length; i++){
          var num = parseInt(opts[i].text); // get the integer value of displayed string
          if ("" + num != "NaN")  // check if it is a number and add to array
              values[values.length] = num;
       }
       return values.sort(function(a,b){return a-b;}).pop(); // sort it ascending numerically and return last value.
    }
    </browser>
    
    // This will set the select box with the option with maximum displayed value.
    _setSelected(_select("leaveReqNo"),  ""+getMaxOption(_select("leaveReqNo")));
    

    Regards,
    Narayan
  • srinisrini Members
    Thanks Narayan..... now i am able to pick max dropdown value....Thanks for your support.....
This discussion has been closed.