问题描述
我的旧 WinForm 应用程序使用 HtmlElementCollection 处理页面
My old WinForm application used HtmlElementCollection to process a page
HtmlElementCollection hec = this.webbrowser.Document.GetElementsByTagName("input");
在 WPF WebBrowser 中,有几处不同.例如
In WPF WebBrowser, there are several things that are different. For example
this.webbrowser.Document 没有任何名为 GetElementsByTagName
因此我的代码无法获得 HtmlElementCollection
Therefore my code is unable to get an HtmlElementCollection
推荐答案
您需要添加对 Microsoft.mshtml
的引用,然后您需要将文档转换为 mshtml.HTMLDocument
>.完成此操作后,您应该可以使用 getElementsByTagName()
方法
You need to add reference to Microsoft.mshtml
and then you need to cast document as mshtml.HTMLDocument
. After you do that you should be able to use getElementsByTagName()
method
var document = webBrowser.Document as mshtml.HTMLDocument;
var inputs = document.getElementsByTagName("input");
foreach (mshtml.IHTMLElement element in inputs)
{
}
getElementsByTagName()
返回 mshtml.IHTMLElementCollection
并且每个项目都是 mshtml.IHTMLElement
类型
getElementsByTagName()
returns mshtml.IHTMLElementCollection
and each item is of a mshtml.IHTMLElement
type
编辑
替代解决方案,如果您需要使用 WinForms WebBrowser
,您可以使用它而不是 WPF.添加对WindowsFormsIntegration
和System.Windows.Forms
的引用,在XAML 中创建命名空间并使用不同的浏览器控件
Alternative solution, if you need to use WinForms WebBrowser
you can use that instead of the WPF one. Add reference to WindowsFormsIntegration
and System.Windows.Forms
, create namespace in XAML and use different browser control
<Window ...
xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
<WindowsFormsHost>
<winforms:WebBrowser x:Name="webBrowser"/>
</WindowsFormsHost>
</Window>
这篇关于如何从 WPF WebBrowser 获取 HtmlElementCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!