Using selenium.getBodyText () to capture an HTML source using Java, how can I save it to an HTML file locally?

This is probably a java noob question, but here is my script:

  • Using selenium, I grabbed the html source using getBodyText ()
  • using java, I want to save the information from getBodyText () into an html file, so that I can view it later

I currently have getBodyText (), which is stored as a string, here is the code:

String stored_report = selenium.getBodyText();

File f = new File("C:/folder/" + "report" + ".html");
FileWriter writer = new FileWriter(f);
writer.append(stored_report);
System.out.println("Report Created is in Location : " + f.getAbsolutePath())
writer.close();

Do I need to use FileReader? What do I need to do so that the saved html file still displays the html format? (currently, since it is stored as a string, the page is displayed with everything that is displayed on one line)

Thanks in advance!

+3
source share
1

:

String stored_report = selenium.getBodyText();

File f = new File("C:/folder/" + "report" + ".html");
FileWriter writer = new FileWriter(f,true);
writer.write(stored_report);
System.out.println("Report Created is in Location : " + f.getAbsolutePath())
writer.close();

, . FileWriter (f, true) .

, , .

: , selenium.getHtmlSource() , . .

+5

Source: https://habr.com/ru/post/1710448/


All Articles