【问题标题】:How to get index of column in table by name using WebElement selenium C#如何使用WebElement selenium C#按名称获取表中列的索引
【发布时间】:2016-04-28 21:56:02
【问题描述】:

我试图按名称获取列(或行)表的索引,但我正在使用 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
    }
}

【问题讨论】:

  • 请添加表格html
  • 如何添加?可以帮我演示一下吗
  • 点击问题下方的“编辑”并将其添加到您的问题中。

标签: c# selenium html-table


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2017-02-02
    • 2015-10-02
    • 2015-10-18
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多