How can I get HtmlElementCollection from WPF WebBrowser

My old WinForm application was using HtmlElementCollection to render the page

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

      

There are several things in WPF WebBrowser that are different. for example

this.webbrowser.Document does not have a GetElementsByTagName method

So my code cannot get HtmlElementCollection

+3


source to share


1 answer


You need to add a link to Microsoft.mshtml

, and then you need to make the document as mshtml.HTMLDocument

. Then you can use the methodgetElementsByTagName()

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

 }

      

getElementsByTagName()

returns mshtml.IHTMLElementCollection

and each element is of typemshtml.IHTMLElement



EDIT

Alternative solution, if you need to use WinForms WebBrowser

, you can use this instead of WPF. Add reference to WindowsFormsIntegration

and System.Windows.Forms

, create namespace in XAML and use different browser controls

<Window ...
    xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <WindowsFormsHost>
        <winforms:WebBrowser x:Name="webBrowser"/>
    </WindowsFormsHost>
</Window>

      

+6


source







All Articles