0

I am attempting to pull some values from a webpage, with the intention of writing them into a .txt file for manual validation. I have looked around the web and cannot find the way to achieve this in my scenario. I will be writing the code in java if possible.

I have the following html code available for the element:

<td class="value" data-bind="text: 
$data.value">Windows Server 2012 R2 Standard 64-bit</td>

And the xpath for the element is:

html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]

Is anyone able to help me create a sample piece of code that will;

a) Pick up the value and write it into a text file. Preferably with a prefix of 'Operating system'.
b) Save the file with a unique ID, My thought is to suffix the filename with a datetime stamp.
c) I will have multiple elements to read from the webpage and then write to the text file, around 8 or so, is there any consideration I need to be aware of for writing multiple values to a .txt file and format them neatly?

Hopefully I have included everything I need to here, if not just ask!

Many thanks in advance. KG

KG1984
  • 27
  • 1
  • 2
  • 6
  • What is the big picture? Why are you trying to write information to a local text file? – ErstwhileIII Aug 12 '14 at 12:54
  • Are you basically just asking http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file ? – Mardoz Aug 12 '14 at 12:56
  • I am by no means a pro with selenium or indeed java as you may have deduced from the question. What I need to do is validate values on a web application and then manually validate them from within a windows application (oracle). I'm taking my first steps here so have not considered, at this stage, adding further code to check the windows application using the automation tool, as I believe this will be a whole different language to find the values in a windows application. – KG1984 Aug 12 '14 at 12:59
  • @Mardoz - I am just reviewing the answers in the question you have provided to see if they can do what I require. I will update here shortly. Cheers – KG1984 Aug 12 '14 at 13:13

2 Answers2

0

I have reviewed the answers on the question How do I create a file and write to it in Java?, thanks for the nudge @Mardoz, and with some playing around I have it doing what I needed. Here is the final code, with the date also being tagged into the filename:

Date date = new Date() ;
    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-yyyy HH-mm") ;

    //      Wait for the element to be available
    new WebDriverWait(Login.driver,10).until(ExpectedConditions.visibilityOfElementLocated
            (By.xpath("html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]")));

    Writer writer = null;

    //      Find the value and write it to the text file 'Smoke_004 DD-MM-yyyy HH-mm.txt'
    try {
        writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("Smoke_004 " + dateFormat.format(date) + ".txt"), "utf-8"));
        writer.write("Operating System : " + Login.driver.findElement
                (By.xpath("html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]"))
                    .getText());

           } catch (IOException ex) {
        // report
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }

Thanks for your input @Mardoz and @ErstwhileIII

Community
  • 1
  • 1
KG1984
  • 27
  • 1
  • 2
  • 6
0

Thats not as difficult as it seems. I use similar function for me to write a log into a txt file.

At first I would write all Information in one String variable. It's helpful to format the Information before writing it into the variable. If you collect all Informations your could write this String very simple to a txt file using the following Code:

private static void printToTxt(){

String info = "Collected Informations";
String idForTxtFile = new SimpleDateFormat("dd.MM.yyyy_HH.mm.ss").format(new Date());
File file = new File("Filename" + idForTxtFile);

try {
  FileWriter fw = new FileWriter(file, true);

  //if you want to write the linesperator ("\n) as they are in the txt you should use the following Code:

  String lineSeparator = System.getProperty("line.separator");
  String[] ouput = info.split("\n");

  for (int i = 0; i <= output.length-1; i++) {
    fw.write(output[i]);
    fw.write(lineSeparator);
  }

  //instead you could only use:
  fw.write(info);

  fw.flush();
  fw.close();
} catch (IOException e) {
  e.printStackTrace();
  System.out.println(e.getLocalizedMessage);
}

Little bit late, but here is my version, maybe you could use some additional to yours!

Pascal
  • 74
  • 8
  • Thanks for the additional info Pascal. I have pulled the line.seperator piece out and included into mine for the multiple line formatting, Cheers (tried to +1 but I need 2 more rep myself to do that :D apologies) – KG1984 Aug 12 '14 at 13:59
  • 1
    Accepted this answer due the inclusion of the txt file formatting, which has enhanced my original self answer. – KG1984 Aug 13 '14 at 08:32