0
C#の新機能で、Webサイトのデータテーブルからデータを取得し、csvファイルに保存しようとしています。今まで私はcsvファイルにデータを取得することができましたが、各レコードは列Aの新しいセルに追加されます(Excelで表示)。すなわち..UIテーブルのデータをCSVファイルに書き込む
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);
}
}
}
最終的な目標は、予想通りのUIテーブル内のデータがあることを確認するために、別の「期待される成果」CSVで、このCSVファイルを比較することです。 2つのファイルを比較するより効率的な方法がある場合、つまり配列を使用して結果のCSVと比較すると、私は提案にオープンしています。
ご協力いただければ幸いです。
ブリリアント、ありがとう!これはもう少しですが、今は別の列の値を扱う必要がありますが、列3のデータは2つの列に分割するコンマ(つまり51,100.00)の数値を持つため、別の方法で処理する必要があります。 – alex
実際には、引用符で囲み表記をラップしようとしましたが、行=行+文字列。形式( "{0}"、 "\" "+ tableData +" \ ""); – alex
コンマ区切りファイルの代わりに、パイプ記号、またはデータの一部として使用されていない記号を使用することができます。ファイルをインポートする際に、そのシンボルを区切り文字として指定することができます。 –