Get HTML source code from browser control embedded in C#

二次信任 提交于 2019-12-06 05:07:20

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;

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

Add a Navigated event to your WebBrowser. Only then will your document be filled.

    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        Console.WriteLine(webBrowser1.DocumentText);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!