使用Watin在下拉列表中选择每个项目

本文关键字:项目 选择 Watin 下拉列表 使用 | 更新日期: 2023-09-27 18:02:20

我想使用Watin遍历下拉列表。HTML看起来像这样:

<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>

当你点击下拉菜单中的一个选项时,它会加载一个不同的页面,我想要依次加载它们。基本上,我想做这样的事情:

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

但是我很确定我的代码是错的

使用Watin在下拉列表中选择每个项目

当循环遍历项列表时,有时我遇到对象超出作用域的情况,特别是在使用Page Object模式时(使用它-它很棒!)。因此,我倾向于使用显式声明的count来循环,而不是引用要循环的列表。额外的好处:将count存储在变量中并使用该变量比每次引用浏览器对象要快;如果你有大量的项目要循环,这很重要。

一些粗糙的未完成的代码-基本上是alonp说的充实了一点:

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();
}