Monday, 7 October 2013

Write into Existing CSV file | Selenium

Writing values into CSV is simple.

FileWriter writer = new FileWriter("C:\\Test\\output.csv"); // windows machine
writer.append("ColumnHeader1");
writer.append(',');
writer.append("ColumnHeader2");
writer.append('\n');

for(Value)
{
String element1 = driver.findElement(By.id(Value)).getAttribute("title"); // Example
String element2 = driver.findElement(By.id(Value)).getAttribute("title"); // Example
writer.append(element1);
writer.append(',');
writer.append(element2);
writer.append('\n');
writer.flush();
}

5 comments:

  1. what is this in C#?

    ReplyDelete
  2. Hi Prashanth,

    In the following code snippet,
    for(Value)
    {
    String element1 = driver.findElement(By.id(Value)).getAttribute("title"); // Example
    String element2 = driver.findElement(By.id(Value)).getAttribute("title"); // Example
    writer.append(element1);
    writer.append(',');
    writer.append(element2);
    writer.append('\n');
    writer.flush();
    }


    what is that for(Value) value stands for ???

    ReplyDelete
  3. Very helpful. thx

    ReplyDelete
  4. What will happen if the csv already has data in it and you want to add more?

    ReplyDelete