2017-05-24 50 views
1

我試圖找到它由數據放置在列1列11硒C#Find鏈接

很多狩獵圍繞我發現下面的代碼的這段適合我的需求後,在一個表中的鏈接

 IWebElement table = driver.FindElement(By.XPath("//*[@id='NewBusinessDetailRecords']")); 
     IList <IWebElement> rows = table.FindElements(By.TagName("tr")); 

     foreach (IWebElement row in table) 
     { 
      rows = table.FindElements(By.TagName("tr")); 
      IList<IWebElement> cells = row.FindElements(By.TagName("td")); 
      if (cells[10].Text.Equals("103")) 
      { 
       cells[0].Click(); 
      } 
     } 

然而foreach語句不工作突出表錯誤

foreach語句無法在類型「OpenQA.Selenium.IWebElement」,因爲「OpenQA.Sele變量的操作nium.IWebElement」不包含一個公共定義‘的GetEnumerator’

幾個職位建議我需要使用的IEnumerable(也有一些這表明這應該是自動的),但我沒有設法制定出實現這一進入我的代碼。

任何幫助表示讚賞

編輯:

例HTML 1和錶行

<table id="NewBusinessDetailRecords" cellspacing="0" style="width:100%;" class="listviewgrid"> 
<thead> ... </thead>  
<tbody>   
<tr id="0" class="datagriddetail">  
<td style="text-align: center">    
<a href="" accesskey="1" style="cursor: pointer; cursor: hand;" onclick="CopyNewBusinessDetailRecord(0, 0, 1083406, 14436); return false;" title="Matched to Invoice with ID = 14436; Client with ID = 1083406"><img src="../../images/icons/invoice.png" border="0"></a>    </td>   
<td> Test1 Case1 </td>  
<td> Invoice </td>  
<td> GBP </td>  
<td style="text-align: right"> 600.00 </td>  
<td> 0% </td>  
<td style="text-align: right"> 600.00 </td>  
<td> </td>  
<td> Company Name </td>  
<td> UserName</td>  
<td> 103 </td>  
<td> </td>  
<td> </td>  
<td> </td>  
<td style="text-align: center">   </td>  
</tr>  <tr id="0" class="datagriddetail">  

回答

0

你想重複在<table>每個<tr>。你應該通過rows進行迭代,不table

foreach (IWebElement row in rows) 

此外,你需要刪除這個多餘的線:rows = table.FindElements(By.TagName("tr"));。一旦「內部」 foreach循環中,row變量包含<tr>IWebElement進行處理。


根據您的樣本HTML,我也有幾點建議:

  • 我會避免可能的情況下使用XPath。在你的情況下,只需按ID選擇。

    IWebElement table = driver.FindElement(By.Id("NewBusinessDetailRecords")); 
    
  • 在索引cells之前添加驗證。這會阻止ArgumentOutOfRangeException

    if (cells.Count > 10 && cells[10].Text.Equals("103")) 
    

全碼:

IWebElement table = driver.FindElement(By.Id("NewBusinessDetailRecords")); 
IList<IWebElement> rows = table.FindElements(By.TagName("tr")); 

foreach (IWebElement row in rows) 
{ 
    IList<IWebElement> cells = row.FindElements(By.TagName("td")); 
    if (cells.Count > 10 && cells[10].Text.Equals("103")) 
    { 
     cells[0].Click(); 
    } 
} 
+0

完美的作品! – Smithy7876

1

你有這樣的錯誤,因爲foreach只與IEnumerable類型

foreach (IWebElement row in table)工作肯定會失敗,因爲tableIWebElement,它顯然是一個單一的對象。

你需要什麼這裏只是刪除不必要的foreach循環,像這樣(0行第10列):

var row = driver.FindElement(By.CssSelector("table#NewBusinessDetailRecords tr#0")); 
var cells = row.FindElements(By.TagName("td")).ToList(); 
if (cells[10].Text.Equals("103")) 
{ 
    cells[10].Click(); 
} 

我不能告訴你是肯定的,因爲我沒有的HTML代碼元素,你需要點擊,但我認爲所有的代碼可以用這個來代替:

driver.FindElement(By.LinkText("103")).Click(); 

或者,如果你想指定鏈接文本中的一部分:

driver.FindElement(By.PartialLinkText("10")).Click(); 
+0

該解決方案是不適合我的工作,因爲我就行,如果得到一個ArgumentOurOfRangeExpection。我會將一些示例Html附加到我的問題 – Smithy7876

+0

我無法在html中找到鏈接代碼。我認爲它看起來像103。在這種情況下,最後一個代碼片段應該可以工作:driver.FindElement(By.LinkText(「103」))。Click();. –

+0

該鏈接嵌入在 Smithy7876