如何显示要在真实浏览器中打开的 HtmlPage 对象(来自 HtmlUnit)?

发布于 2024-08-30 18:09:22 字数 130 浏览 2 评论 0原文

我正在使用 Htmlunit(浏览器自动化/测试工具)来浏览一系列链接或在页面上执行一组操作。 在这之后的某个时刻,我想在浏览器(Internet Explorer 或 Firefox 等)中查看结果页面。 我该怎么办呢。 ?谢谢各位朋友...

I am using Htmlunit (browser automation/testing tool) to go through a series of links or perform a set of actions on a page.
At some point after this I want to see the resulting page in a browser (internet explorer or firefox etc.)
How can I do this. ? Thank you Friends...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分開簡單 2024-09-06 18:09:22

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

You can also use htmlPage.save(File) (which automatically saves images) before executing real browser

梦里°也失望 2024-09-06 18:09:22

我希望我正确地理解了你。

这是我的解决方案:

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);

希望有帮助。

I hope I got you correctly.

This is my solution:

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);

Hope that helps.

风吹雪碎 2024-09-06 18:09:22

我想这就是他的想法

//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);

I think this is what he had in mind

//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);
依 靠 2024-09-06 18:09:22

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

示例:

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

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

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

After saving the page to a file with the HtmlPage.save(File) method,
you can use the java.awt.Desktop API to open the file in its default application on your system (i.e. web browser if the file extension is ".html")

Example:

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

This should work on any OS/browser.

Credit: thanks to this answer for suggesting the java.awt.Desktop solution for launching a web browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文