2016-01-22 63 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

相关问题