2012-03-15 67 views
0

我在看的場景是我們有一個包含多個列的表格。其中一列有名稱,另一列有下拉列表。我需要操縱包含特定名稱的行的下拉菜單。我查看了源輸出,並嘗試獲取元素的祖父(表格行),以便我可以搜索列表。但是,當我使用父對象時,沒有這種搜索功能。找到WatiN父母的兄弟元素的孩子

似乎在自動化/測試網站上會有很多這種場景,但是我搜索了幾個小時後還沒有找到任何東西。任何幫助,將不勝感激。

編輯:有問題的應用程序是一個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> 
+0

您能否提供您正在搜索的HTML樣本? – MotoSV 2012-03-15 21:43:16

+0

編輯帖子添加HTML樣本。 – iAmMe 2012-03-16 20:26:21

回答

0

我找到了一種方法來做我想做的事。這可能不是最好的或最優雅的解決方案,但它可以工作(它不是生產代碼)。

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

希望這可以爲別人節省一點時間和精力。另外,我很想看看有人有更好的解決方案。

相關問題