Java – how to save a complete web page

Is there any way to save a complete web page using webdriver?

At present, I do getpagesource (), and then put all the contents into the HTML local file, but the saved page is in poor state (strange characters, no image, all elements offset downward)

See the code I used below:

@Test
   public void testSomeThing(){
     FirefoxDriver driver = new FirefoxDriver();
     driver.get("http://google.com");
     String pageSource = driver.getPageSource();
     writeInFile(System.getProperty("user.dir")+"/target/logs/testPage.html",pageSource);
   }

   public static void writeInFile(String sFileName,String sTextToWrite){
         FileWriter outFile;
         General.sendComments("Write to file: " + sFileName);
         try {
                outFile = new FileWriter(sFileName);
                PrintWriter out = new PrintWriter(outFile);
                out.print(sTextToWrite);
                out.close();
         } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }
   }

Can someone suggest that I use webdriver to save a complete web page in Firefox? For example, automatic?

Solution

Strange characters may be related to the encoding written to the file

Other problems may be related to the fact that you are loading a static HTML file, and the relative URL no longer points to anything Any JavaScript, CSS, and image files will be lost

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>