3

My old WinForm application used HtmlElementCollection to process a page

HtmlElementCollection hec = this.webbrowser.Document.GetElementsByTagName("input");

In WPF WebBrowser, there are several things that are different. For example

this.webbrowser.Document does not have any method called GetElementsByTagName

Therefore my code is unable to get an HtmlElementCollection

4

1 回答 1

6

您需要添加对的引用Microsoft.mshtml,然后您需要将文档转换为mshtml.HTMLDocument. 完成后,您应该可以使用getElementsByTagName()方法

 var document = webBrowser.Document as mshtml.HTMLDocument;
 var inputs = document.getElementsByTagName("input");
 foreach (mshtml.IHTMLElement element in inputs)
 {

 }

getElementsByTagName()返回mshtml.IHTMLElementCollection并且每个项目都是一个mshtml.IHTMLElement类型

编辑

替代解决方案,如果您需要使用 WinForms WebBrowser,您可以使用它而不是 WPF 之一。添加对WindowsFormsIntegrationand的引用System.Windows.Forms,在 XAML 中创建命名空间并使用不同的浏览器控件

<Window ...
    xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <WindowsFormsHost>
        <winforms:WebBrowser x:Name="webBrowser"/>
    </WindowsFormsHost>
</Window>
于 2015-04-30T16:23:56.543 回答