如何使用WebElement selenium C#

时间:2016-01-22 10:24:18

标签: c# selenium html-table

我试图按名称获取列(或行)表的索引,但我使用selenium WebElement来捕获表或列,行,....的接口,我无法存储捕获表到DataTable(在System.Data.dll引用中)。我在get index of DataTable column with name中读到了,但我无法将数据存储在表格列DataColumn中,并使用DataColumn.Ordinal获取。这是我的界面:

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 个答案:

答案 0 :(得分: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;
}
}

你看,列的索引是“i”:D