java Selenium - 将值写入 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25264767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:43:38  来源:igfitidea点击:

Selenium - writing values to a .txt file

javaselenium

提问by KG1984

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.

我试图从网页中提取一些值,目的是将它们写入 .txt 文件以进行手动验证。我环顾了网络,找不到在我的场景中实现这一目标的方法。如果可能,我将用 Java 编写代码。

I have the following html code available for the element:

我有以下可用于元素的 html 代码:

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

And the xpath for the element is:

元素的 xpath 是:

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?

a) 选取值并将其写入文本文件。最好带有前缀“操作系统”。
b) 使用唯一 ID 保存文件,我的想法是在文件名后加上日期时间戳。
c) 我将从网页中读取多个元素,然后写入文本文件,大约 8 个左右,是否需要注意将多个值写入 .txt 文件并整齐地格式化它们?

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

希望我已经包括了我需要的一切,如果不是只是问的话!

Many thanks in advance. KG

提前谢谢了。公斤

采纳答案by Pascal

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

那并不像看起来那么困难。我使用类似的功能将日志写入txt文件。

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:

起初,我会将所有信息写入一个字符串变量中。在将信息写入变量之前对其进行格式化是有帮助的。如果您收集所有信息,您可以使用以下代码将此字符串非常简单地写入 txt 文件:

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!

有点晚了,但这是我的版本,也许你可以使用一些额外的!

回答by KG1984

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:

我已经查看了有关如何创建文件并用 Java 写入文件的问题的答案,感谢您对@Mardoz 的推动,在玩了一些游戏之后,我就可以做我需要的事情了。这是最终的代码,日期也被标记到文件名中:

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

感谢您的输入 @Mardoz 和 @ErstwhileIII