Select each item in dropdown list using Watin

安稳与你 提交于 2019-12-12 05:37:10

问题


I want to iterate through a dropdown list using Watin. The HTML looks like this:

<select name="ctl00$Header1$ddlPropertyList" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$Header1$ddlPropertyList\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_Header1_ddlPropertyList" onmouseover="this.title=this.options[this.selectedIndex].title" style="width:325px;">
    <option selected="selected" value="0185795046:R:GPC:Eligible:F" title="0185795046 - ">0185795046 - </option>
    <option value="0325844068:R:GPC:Eligible:F" title="0325844068 - ">0325844068 - </option>
    <option value="0374795034:R:GPC:Eligible:F" title="0374795034 - ">0374795034 - </option>
    <option value="0510031035:C:GPC:Eligible:F" title="0510031035 - ">0510031035 - </option>
    <option value="1424795158:R:GPC:InEligible:F" title="1424795158 - ">1424795158 - </option>
    <option value="1550795037:R:GPC:Eligible:F" title="1550795037 - ">1550795037 - </option>

</select>

When you click on one of the options in the dropdown, it loads a different page, and I want to load each of them in succession. Basically, I'm trying to do something like this:

SelectList ddl = browser.SelectList(Find.ById("ctl00$Header1$ddlPropertyList"));
            foreach (var item in ddl.AllContents)
            {
                ddl.Select(item);
            }

But I'm pretty sure my code is just wrong.


回答1:


When looping through a list of items, sometimes I've run into cases where objects are out of scope, especially when using the Page Object pattern (use it - it is great!). So I tend to use looping an explicitly declared count rather than referencing the list to loop through. Added bonus: Storing count in a variable and using that variable is quicker than referencing the browser object each time; makes a difference if you have a large number of items to loop through.

Some rough unfinished code - basically what alonp said fleshed out a bit more:

int numberOfItems = browser.SelectList(Find.ById("ctl00$Header1$ddlPropertyList")).count;

for(int i = 0; i < numberOfItems; i++)
{
    //this is one the "search" page
    browser.SelectList(Find.ById("ctl00$Header1$ddlPropertyList")).Options[i].Select;
    browser.yourGoAction();   <- assumes navigation isn't automatic when an item is selected.  EG:  button.Click() or something.

    //this is on the "results" page.
    do stuff

    //go back to the "search" page.
    browser.Back();
}


来源:https://stackoverflow.com/questions/15236339/select-each-item-in-dropdown-list-using-watin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!