使用 WatiN 查找父元素的同级元素的子元素 [英] Finding the child of a parent's sibling element with WatiN

查看:49
本文介绍了使用 WatiN 查找父元素的同级元素的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的场景是我们有一个包含多列的表.其中一列有名称,另一列有下拉列表.我需要操作包含特定名称的行的下拉列表.我查看了源输出,并尝试获取元素的祖父级(表行),以便我可以搜索列表.但是,当我使用父对象时,没有这样的搜索功能.

The scenario that I am looking at is that we have a table with multiple columns. One of those columns has a name, another has a dropdown list. I need to manipulate the dropdown for a row that contains a particular name. I looked at the source output, and tried getting the element's grandparent (the table row) so that I could search for the list. However, there was no such search functionality when I used the parent object.

似乎在自动化/测试网站时会出现很多这种情况,但我搜索了几个小时后没有找到任何东西.任何帮助将不胜感激.

It seems like there would be a lot of this kind of scenario in automating/testing a site, but I have not found anything after searching for a couple of hours. Any help would be appreciated.

有问题的应用程序是一个 ASP.NET,输出的 HTML 充其量是粗糙的.但是,这里有一个经过清理的示例,展示了正在搜索的 HTML 的样子:

The application in question is an ASP.NET, and the output HTML is gnarly at best. However, here is a cleaned up example of what the HTML being searched looks like:

<table class="myGrid" cellspacing="0" cellpadding="3" rules="all" border="1" id="ctl00_content_MyRpt_ctl01_MyGrid" style="border-collapse:collapse;">
  <tr align="left" style="color:Black;background-color:#DFDBDB;">
    <th scope="col">Name</th><th scope="col">Unit</th><th scope="col">Status</th><th scope="col">Action</th>
  </tr>
  <tr>
    <td>
      <span id="ctl00_content_MyRpt_ctl01_MyGrid_ctl02_Name">JOHN DOE</span>
    </td>
    <td>
      <span id="ctl00_content_MyRpt_ctl01_MyGrid_ctl02_UnitType">Region</span>&nbsp;
      <span id="ctl00_content_MyRpt_ctl01_MyGrid_ctl02_UnitNum">1</span> 
    </td>
    <td>
      <span id="ctl00_content_MyRpt_ctl01_MyGrid_ctl02_Status">Complete</span>                                   
    </td>
    <td class="dropdown">                                                          
      <select name="ctl00$content$MyRpt$ctl01$MyGrid$ctl02$ActionDropDown" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$content$MyRpt$ctl01$MyGrid$ctl02$ActionDropDown\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_content_MyRpt_ctl01_MyGrid_ctl02_ActionDropDown" class="dropdown">
        <option value="123456">I want to...</option>
        <option value="Details.aspx">View Details</option>
        <option value="Summary.aspx">View Summary</option>
        <option value="DirectReports.aspx">View Direct Reports</option>
      </select>
    </td>
  </tr>
  <tr>
    ...
  </tr>
</table>

推荐答案

我找到了一种方法来做我想做的事.这可能不是最好或最优雅的解决方案,但它有效(它不是生产代码).

I found a way to do what I wanted. It is probably not the best or most elegant solution, but it works (it is not production code).

    private void btnStart_Click(object sender, EventArgs e)
    {
        using (var browser = new IE("http://godev/review"))
        {
            browser.Link(Find.ByText("My Direct Reports")).Click();
            TableRow tr = browser.Span(Find.ByText("JOHN DOE")).Parent.Parent as TableRow;
            SelectList objSL = null;
            if (tr.Exists)
            {
                foreach (var td in tr.TableCells)
                {
                    objSL = td.ChildOfType<SelectList>(Find.Any) as SelectList;
                    if (objSL.Exists) break;
                }
                if (objSL != null && objSL.Exists)
                {
                    Option o = objSL.Option(Find.ByText("View Direct Reports"));
                    if (o.Exists) o.Select();
                }
            }
        }
    }

希望这可以为某人节省一点时间和精力.另外,我很想看看是否有人有更好的解决方案.

Hopefully this saves someone a little time and effort. Also, I would love to see if someone has a better solution.

这篇关于使用 WatiN 查找父元素的同级元素的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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