2016-01-22 65 views
2

我試圖通過名稱獲得列(或行)表的索引,但我使用硒WebElement來捕獲表或列,行的接口。 ...我無法將捕獲的表存儲到DataTable(在System.Data.dll參考中)。我在get index of DataTable column with name中讀到,但是我不能將表中的數據存儲到DataColumn的列中,並使用DataColumn.Ordinal來獲取它。下面是我的界面:如何通過名稱使用WebElement硒獲取表中列的索引C#

IWebElement Table = driver.FindElement(By.XPath("//*[@class='DivTable']//table")); 
ReadOnlyCollection<IWebElement> allRows = Table.FindElements(By.TagName("tr")); 
foreach(IWebElement row in allRows) 
{ 
    ReadOnlyCollection<IWebElement> allCols = row.FindElements(By.TagName("td")); 
    foreach (IWebElement col in allCols) 
    { 
     //do something 
    } 
} 
+1

請添加表格html – Guy

+0

我該如何添加?您可以爲我演示,請 –

+0

點擊您的問題下的「編輯」,並將其添加到您的問題。 – Guy

回答

1

我做到了,我想分享給大家誰是初學者和我一樣:d

String sColValue = "Licensing"; 


//First loop will find the 'ClOCK TWER HOTEL' in the first column 
for (int i=1;i<=allCols.Count;i++){ 
    string sValue = null; 
    sValue = driver.FindElement(By.XPath(".//*[@id='post-2924']/div/table/tbody/tr[1]/th["+i+"]")).Text; 
    if(sValue.Equals(sColValue)){ 

     // If the sValue match with the description, it will initiate one more inner loop for all the columns of 'i' row 
     for (int j=1;j<=2;j++){ 
      string sRowValue= driver.FindElement(By.XPath(".//*[@id='post-2924']/div/table/tbody/tr["+j+"]/td["+i+"]")).Text; 
      Console.WriteLine(sRowValue); 
     } 
    break; 
} 
} 

你看,列的索引是「我」 :D

相關問題