1

我正在使用 Htmlunit(浏览器自动化/测试工具)来浏览一系列链接或在页面上执行一组操作。在此之后的某个时候,我想在浏览器(Internet Explorer 或 firefox 等)中查看生成的页面。我该怎么做。? 谢谢朋友...

4

4 回答 4

1

您还可以在执行真实浏览器之前使用 htmlPage.save(File) (自动保存图像)

于 2010-05-08T14:42:51.667 回答
1

我希望我能正确理解你。

这是我的解决方案:

WebClient wc = new WebClient();

HtmlPage page = wc.getPage("http://stackoverflow.com/");

//Get page as Html
String htmlBody = page.getWebResponse().getContentAsString();

//Save the response in a file
String filePath = "c:/temp/out.html";
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath)));
bw.write(htmlBody);
bw.close();

//Open the page with a browser
Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/iexplore.exe " + filePath);

希望有帮助。

于 2010-05-06T06:46:40.770 回答
1

我想这就是他的想法

//Get page as Html <br>
HtmlPage page = wc.getPage("http://stackoverflow.com/");

//create File object <br>
File file = new File("c://temp//out");

//save page image <br>
page.save(file);
于 2012-12-19T23:57:09.190 回答
0

使用该方法将页面保存到文件后HtmlPage.save(File),您可以使用java.awt.DesktopAPI 在系统上的默认应用程序中打开该文件(即,如果文件扩展名为“.html”,则为 Web 浏览器)

例子:

HtmlPage page = webClient.getPage("http://example.com/");
File file = File.createTempFile("example", ".html");
page.save(file);
Desktop.getDesktop().open(file);

这应该适用于任何操作系统/浏览器。

学分:感谢这个答案建议java.awt.Desktop启动网络浏览器的解决方案。

于 2020-03-06T02:36:32.377 回答