将UI表数据写入CSV文件

时间:2016-09-30 08:53:22

标签: c# csv selenium

C#的新手,我正在尝试从网站上的数据表中获取数据并将其保存到csv文件中。到目前为止,我已设法将数据输入csv文件,但每个记录都附加到A列中的新单元格(如Excel中所示)。即...

  A           B           C
1 A_record1
2 A_record2
3 A_record3
4 B_record1
5 B_record2
6 B_record3

我想将数据放在csv文件中,格式为......

  A           B           C
1 A_record1   A_record2   A_record3
2 B_record1   B_record2   B_record3

我在第一个例子中填充csv的代码是......

//divided xpath In three parts to pass Row_count and Col_count values.
String firstPart = "//div[@id='lwDataGrid']/table/tbody/tr[";
String secondPart = "]/td[";
String thirdPart = "]";

//Row and Column counts
int rowCount = driver.FindElements(By.XPath("//div[@id='lwDataGrid']/table/tbody/tr[*]")).Count - 3;
int colCount = driver.FindElements(By.XPath("//div[@id='lwDataGrid']/table/tbody/tr[1]/td")).Count;
System.Diagnostics.Debug.WriteLine(rowCount + ": This is the number of rows in the table");
System.Diagnostics.Debug.WriteLine(colCount + ": This is the number of columns in the table");

string path = @"C:\...\my.csv";

for (int j = 2; j <= colCount; j++)
{
    //Used for loop for number of columns.
    for (int i = 4; i <= rowCount; i++)
    {
        //Prepared final xpath of specific cell as per values of i and j.
        String finalXpath = firstPart + i + secondPart + j + thirdPart;
        //Will retrieve value from located cell and print It.
        String tableData = driver.FindElement(By.XPath(finalXpath)).Text; 
        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine(tableData);
        }
    }
}

最终目标是将此csv与另一个&#34;预期结果进行比较&#34; csv检查UI表中的数据是否符合预期。如果有比比较两个文件更有效的方法,即使用数组与结果csv进行比较,我可以接受建议。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

从 -

更改您的代码
for (int j = 2; j <= colCount; j++)
{
    //Used for loop for number of columns.
    for (int i = 4; i <= rowCount; i++)
    {
        //Prepared final xpath of specific cell as per values of i and j.
        String finalXpath = firstPart + i + secondPart + j + thirdPart;
        //Will retrieve value from located cell and print It.
        String tableData = driver.FindElement(By.XPath(finalXpath)).Text; 
        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine(tableData);
        }
    }
}

以下可能会解决问题

    for (int j = 2; j <= colCount; j++)
        {
            using (StreamWriter sw = File.AppendText(path))
            {
                string line = string.Empty;
                //Used for loop for number of columns.
                for (int i = 4; i <= rowCount; i++)
                {
                    //Prepared final xpath of specific cell as per values of i and j.
                    String finalXpath = firstPart + i + secondPart + j + thirdPart;
                    //Will retrieve value from located cell and print It.
                    String tableData = driver.FindElement(By.XPath(finalXpath)).Text;
                    line = line + string.Format("{0},",tableData);
                }
                sw.WriteLine(line);
            }
        }