WebBrowser获取一个HtmlElementCollect

WebBrowser获取一个HtmlElementCollect

本文介绍了如何从一个WPF WebBrowser获取一个HtmlElementCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的旧的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

推荐答案

c $ c> 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)
 {

 }


b $ b

getElementsByTagName()返回 mshtml.IHTMLElementCollection ,每个项目都是 mshtml.IHTMLElement type

getElementsByTagName() returns mshtml.IHTMLElementCollection and each item is of a mshtml.IHTMLElement type

EDIT

替代解决方案,如果你需要使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:54