Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTML source code from browser control embedded in C#

I have a browser control embeded in a C# windows app. I want to grab the rendered HTML (which could have been modified by javascript) not the original one.

Any suggestions?

like image 982
Hooch Avatar asked May 09 '26 22:05

Hooch


2 Answers

You can get the HTML, and indeed set it, with WebBrowser.DocumentText.

Sheng is correct, DocumentText returns the streamed document before scripts run. His code doesn't compile, but it's essentially correct. I found that you need:

mshtml.HTMLDocument doc = webBrowser1.Document.DomDocument as mshtml.HTMLDocument;
string html = doc.documentElement.outerHTML;
like image 187
David Heffernan Avatar answered May 11 '26 11:05

David Heffernan


DocumentText internally use the document's IPersistStream interface which returns the original HTML. Use webBrowser1.Document.DocumentElement.OuterHTML instead.

like image 33
Sheng Jiang 蒋晟 Avatar answered May 11 '26 10:05

Sheng Jiang 蒋晟



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!