将当前网页保存在 Watin 中

发布于 2024-09-28 19:00:40 字数 899 浏览 0 评论 0原文

我正在尝试使用 使用 Watin 显示 IE“另存为”对话框

所以这里是:

IE ie = new IE("http:// /本地主机"); // 更多代码

//我期望 out.html 是输出文件 FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("out.html");

//我希望这一行会弹出另存为对话框,但什么也没有发生 即.AddDialogHandler(fileDownloadHandler);

//程序被阻塞在这一行,因为它无法点击任何地方 ie.Link("startDownloadLinkId").Click();

        fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(15); 
        fileDownloadHandler.WaitUntilDownloadCompleted(200); 

我也尝试了下面的代码,但它没有保存所有页面: System.IO.StreamWriter 文件 = new System.IO.StreamWriter("output.html"); file.Write(ie.Html);

同样,我需要从 Watin 保存网页,结果与手动保存相同。

谢谢!

I'm trying to save the full content of the current static web page, using the code from Show IE "Save As" dialog using Watin

So here it is:

IE ie = new IE("http://localhost");
// more code

//I expect out.html is the output file
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("out.html");

//I expect this line to popup the save as dialog box, but nothing happens
ie.AddDialogHandler(fileDownloadHandler);

//the program is blocked at this line, as it can't click anywhere
ie.Link("startDownloadLinkId").Click();

        fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(15); 
        fileDownloadHandler.WaitUntilDownloadCompleted(200); 

I also tried the code below, but it doesn't save all the page:
System.IO.StreamWriter file = new System.IO.StreamWriter("output.html");
file.Write(ie.Html);

Again, I need to save the webpage from Watin, and the result to be the same as saving it manually.

Thanks!

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

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

发布评论

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

评论(2

小猫一只 2024-10-05 19:00:40

我是这样做的:

File.WriteAllText("myfile.html", 
(myIE.InternetExplorer as SHDocVw.InternetExplorer).Document.documentElement.outerHtml);

当然,假设 myIE 是一个 WatiN IE 元素。

如果您在寻找如何使用 WatiN 执行某些操作时遇到困难,我经常发现在 google 上搜索如何使用“Internet Explorer COM 对象”执行此操作会很有帮助。 WatiN 包装了该对象,但它是公开的并且能够被访问!

干杯!

Here is how I do it:

File.WriteAllText("myfile.html", 
(myIE.InternetExplorer as SHDocVw.InternetExplorer).Document.documentElement.outerHtml);

Assumes myIE is a WatiN IE element, of course.

If you're ever having difficulty finding how to do something with WatiN, I often find it helpful to google how to do it with an "Internet Explorer COM object". WatiN wraps the object, but it is exposed and able to be accessed!

Cheers!

圈圈圆圆圈圈 2024-10-05 19:00:40

尝试使用 html agility pack 解析 html 并保存它,您可以使用其他功能...

        using HtmlAgilityPack;

        var htmldoc = new HtmlDocument();
        htmldoc.LoadHtml(ie.Html);
        htmldoc.Save(stream);

链接到 agility pack

Try to parse the html with html agility pack and save it, there are additional abilities that you can use...

        using HtmlAgilityPack;

        var htmldoc = new HtmlDocument();
        htmldoc.LoadHtml(ie.Html);
        htmldoc.Save(stream);

Link to agility pack

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