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.

count the number of options in a select list.

tinchie8tinchie8 Members
edited November -1 in Sahi - Open Source
My post with regard to retrieving the number of options in a select was deleted.

Can anyone please repost it.

Here's my select code:

<select name>
<option>dog</option>
<option>cat</option>
<option>rat</option>
</select>

i want to get the number of options it has.

Thanks a bunch!

Comments

  • Hi tinchie8,

    Try this code...
    function getcount($list){
    return $list.options.length;
    }
    
    _alert(getcount(top.document.getElementById('XYZ')));
    
    Hope this works for you.:)

    Regards,
    Pankaj.
  • tinchie8tinchie8 Members
    Thanks pankaj
  • tinchie8tinchie8 Members
    <select id="animals">
    <option>dog</option>
    <option>cat</option>
    <option>rat</option>
    </select>


    another question though, really forgot to mention this...

    how do I assert if cat is on the options?

    i tried this..

    var $total = getcount(top.document.getElementById('animal'));

    for (var $ctr=0; ctr<$total; $ctr++)
    _assertEqual(_option(_select("animals"), top.document.getElementById('animal').options.value[$ctr] ), "cat");
  • pankaj.nithpankaj.nith Members
    edited May 2008
    Hi,

    Try if this helps:
    function getItem(el, text1) {
        var opts = el.options;
        for (var i = 0; i < opts.length; i++) {
            if (text1 == opts[i].text) return opts[i].text;
        }
        return null;
    }
    
    if (_condition(getItem(_select("animals"), "cat")!=null))
        _alert("Cat exist!");
    else
        _alert("Cat is extinct!");
    
    You can use the function the way you want it.

    Have a nice day!

    Regards,
    Pankaj.
  • tinchie8tinchie8 Members
    Yeey! Thanks!
  • Welcome..:)
  • deepakdeepak Members
    Hi All,

    Could you please help me out in choosing the Frame Work for a web based application.I am planing to automate using SAHI tool.

    Thanks
    Deepak
  • tinchie8tinchie8 Members
    hi deepak, I am not quite sure with frames yet. But I think the notes in Sahi states it is a current issue now. I am also new to automation tools. This one is the best I've used.
Sign In or Register to comment.