How to get the html page from this control so that it can be loaded into HtmlAgilityPack.HtmlDocument? Upon the LoadCompleted event, you can gain access to the property: var doc = this.webBrowser.Document; But the fact is that this property is of type object. How to be?

    1 answer 1

    The WebBrowser.Document property returns the Internel Explorer COM object. You can go 2 ways.

    Use dynamic to access properties of a COM object:

     dynamic doc = wb.Document; string html = doc.documentElement.outerHTML; 

    Or add a reference to Microsoft.mshtml and lead to the HTMLDocument type:

     var doc = wb.Document as mshtml.HTMLDocument; string html = doc.documentElement.outerHTML; 
    • Thank you, but this is not exactly what was expected. I specifically noticed that the WebBrowser is from WPF, and not from WinForms. His Document property is of type object, i.e. only available: ToString (), GetHashCode () and so on. - Bulson
    • @Bulson notice the type of dynamic, it is not the same as a var or object. Document returns object as it should return a COM object, this is in the documentation, read carefully, and dynamic allows you to immediately work with it as an object, but you yourself must know the properties and methods of this object. The second option with an explicit reduction to the desired type. - rdorn
    • @Bulson and also draw your attention to the fact that both WinForms and WPF provide a wrapper over the same COM component of the Internet Explorer engine. - rdorn
    • Yes, thanks for the explanation @rdorn I understood everything. The first option works great. The second will work too. - Bulson
    • Add information from the comments in the answer for completeness, because over time, the number of people who know or even are interested in the intricacies of the internal structure only decreases, unfortunately. - rdorn