【问题标题】:Finding the child of a parent's sibling element with WatiN使用 WatiN 查找父元素的兄弟元素的子元素
【发布时间】:2012-03-15 20:50:36
【问题描述】:

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

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

编辑:有问题的应用程序是一个 ASP.NET,并且输出的 HTML 充其量是粗糙的。然而,下面是一个被搜索的 HTML 的清理示例:

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

【问题讨论】:

  • 您能否提供您正在搜索的 HTML 示例?
  • 编辑帖子以添加 HTML 示例。

标签: c# search watin siblings


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多