如何访问存储在字典中的数组元素?

时间:2016-06-08 18:01:28

标签: c# arrays selenium selenium-webdriver

给定存储在字典中的字符串数组,我想访问存储在数组中的第一个元素。我该怎么做呢?

这是我已经写过的......

public IC2Engineering GetReportResultsTableByXPath(string xPath, int rowNumber) {
        WaitForComplete();

        IWebElement table = FindElement(By.XPath(xPath));
        //get an array of rows from the table
        IList<IWebElement> table_rows = table.FindElements(By.TagName("tr"));
        //store the row data 
        IList<IWebElement> row_data = table_rows[rowNumber].FindElements(By.XPath("td"));

        int col_num;
        col_num = 0;
        foreach (IWebElement cell in row_data) {

            Console.WriteLine("col #" + col_num + ": " + row_data[col_num].Text.ToString());
            col_num++;
        }

        return this;
    }

所以我知道字符串正确存储,因为写入控制台输出的数据是正确的。

如何将来自这些输出中的每一个的数据存储到它自己独立的“#34; spot&#34;在一个名为&#34; TableRowData&#34;?

的SharedProperty Dictionary中

将这些输出存储到字典中的自己位置后,如何访问字典中的第一个条目?

我有一种强烈的感觉,我正在寻找的代码必须在foreach循环内执行,但我无法弄清楚如何做到这一点......

1 个答案:

答案 0 :(得分:0)

如果我理解你的话,问题在于:

SeleniumUITests.SharedProperties["ReportRowData"] = row_data.ToString();

您将“数组”(实际上是List<T>)存储为字典中的字符串,而不是数组本身。看起来你想要:

SeleniumUITests.SharedProperties["ReportRowData"] = row_data;
Console.WriteLine(SeleniumUITests.SharedProperties["ReportRowData"][0]);