使用 Watin 选择下拉列表中的每个项目 [英] Select each item in dropdown list using Watin

查看:34
本文介绍了使用 Watin 选择下拉列表中的每个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Watin 遍历下拉列表.HTML 如下所示:

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.

推荐答案

在循环遍历项目列表时,有时我会遇到对象超出范围的情况,尤其是在使用页面对象模式时(使用它 -太好了!).所以我倾向于使用循环显式声明的计数,而不是引用要循环的列表.额外的好处:将计数存储在变量中并使用该变量比每次都引用浏览器对象更快;如果您有大量要循环的项目,则效果会有所不同.

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.

一些粗略的未完成代码 - 基本上 alonp 所说的更加充实:

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

这篇关于使用 Watin 选择下拉列表中的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆