如何保存完整的网页 [英] How to save complete web page
问题描述
是否可以使用WebDriver保存完整的网页?
Is there any way to save complete web-page using WebDriver?
当前我执行getPageSource(),然后将所有内容放入html本地文件中,但是保存的页面的形状不好(奇怪的字符,没有图像,所有元素向下偏移)
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)
请参阅以下我使用的代码:
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();
}
}
有人可以建议我使用WebDriver在firefox中保存完整网页的方法吗?例如自动?
Could somebody please suggest me the way I can save complete web page in firefox using WebDriver? e.g automatically?
推荐答案
奇怪的字符可能与写入文件的编码有关.
Strange characters might have something to do with the encoding of the written file.
其他问题可能与以下事实有关:正在加载静态html文件,相对url不再指向该文件.所有的javascript,css和图片文件都将丢失.
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. Any javascript, css and image files will be missing.
这篇关于如何保存完整的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!