简体   繁体   English

如何保存完整的网页

[英]How to save complete web page

Is there any way to save complete web-page using WebDriver? 有没有办法使用WebDriver保存完整的网页?

Currently I do getPageSource() and then put everything into html local file, but saved page is in not good shape (strange characters, no images, all elements offset down) 目前我做getPageSource(),然后将所有内容放入html本地文件,但保存的页面状态不佳(奇怪的字符,没有图像,所有元素向下偏移)

See below code that I use: 请参阅下面我使用的代码:

   @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();
         }
   }

Could somebody please suggest me the way I can save complete web page in firefox using WebDriver? 有人可以建议我使用WebDriver在firefox中保存完整网页的方式吗? eg automatically? 例如自动?

Strange characters might have something to do with the encoding of the written file. 奇怪的字符可能与写入文件的编码有关。

The other problems will probably have to do with the fact that you are loading a static html file for which the relative url's no longer point to anything. 其他问题可能与您正在加载静态html文件这一事实有关,而相对URL不再指向任何东西。 Any javascript, css and image files will be missing. 任何javascript,css和图像文件都将丢失。

Savw webPage to a file Try it. Savw webPage到文件试试吧。 but i didn't checked this 但我没有检查过这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM