从 webbrowsercontrol 获取 document.text

发布于 2024-12-21 14:18:33 字数 135 浏览 0 评论 0原文

如何从 WPF 中的 webbrowser 控件获取 document.text?

此控件在 WPF 中不再具有此属性。我查了一下msdn的文章,没有任何相关内容。

另外,谷歌搜索并没有给我任何关于 WPF 中这个控件的结果

How do i get the document.text from the webbrowser control in WPF?

This control doesn't have this property anymore in WPF. I have looked in the msdn articles and there is nothing about it.

Also, googling this up didn't give me any results on this control in WPF

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

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

发布评论

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

评论(1

微凉徒眸意 2024-12-28 14:18:33

浏览器完成导航后,您可以调用这样的方法...

public static void GetPageContent(WebBrowser wb, string fileName)
{
    mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)wb.Document;
    Task t = new Task(() => File.WriteAllText(fileName, dom.body.innerHTML));
    t.Start();
}

该方法将整个页面放入标准的ASCII文件中,您可以根据需要解析出内容。

要使用此方法,您需要包含对 Microsoft.mshtml 程序集的引用,该程序集应位于“添加引用”对话框的 .NET 选项卡中。如图所示,我生成了一个单独的线程以保持 UI 活跃,但这是可选的。

对于您只想将字符串返回给客户端的情况,您可以使用此方法...

public static string GetPageContent(WebBrowser wb)
{
    return ((mshtml.HTMLDocumentClass) wb.Document).body.innerHTML;
}

After the browser has finished navigating, you can call a method like this...

public static void GetPageContent(WebBrowser wb, string fileName)
{
    mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)wb.Document;
    Task t = new Task(() => File.WriteAllText(fileName, dom.body.innerHTML));
    t.Start();
}

This method places the entire page into a standard ASCII file and you can parse out content according to your need.

To use this method, you will need to include a reference to the Microsoft.mshtml assembly, which should be located in the .NET tab of the 'Add Reference' dialog. As shown, I spawn off a separate thread in order to keep the UI perky, but this is optional.

For the case where you simply want the string returned to the client, you can use this method...

public static string GetPageContent(WebBrowser wb)
{
    return ((mshtml.HTMLDocumentClass) wb.Document).body.innerHTML;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文