Search code examples
rubytestingselectwatirdropdown

How to select value in dropdown menu in ruby watir without knowing the value?


I'm trying to select something from a dropdown menu, without knowing what are the options. The code needs to work for many users and everyone has different values.

<select name="aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c$ddlAccount" id="aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c_ddlAccount" class="selectM">
                        <option value="Selectare">Selectare</option>
                        <option value="226523531-Card">226523531-232-MDL</option>

                    </select>

For now I was only found how to do it knowing the options, like this:

browser.option(:text,'226523531-232-MDL').click

Solution

  • I am not sure whether I understood your question correctly,

    If you want to select the first option all the time, then

    locator='aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c$ddlAccount'
    
    b.select_list(name: locator).options.first.select
    

    If you want to select some random option, then

    b.select_list(name: locator).options.to_a.sample.select