WebBrowser - empty DocumentText

前端 未结 5 1072
粉色の甜心
粉色の甜心 2021-01-07 02:16

I\'m trying to use WebBrowser class, but of course it doesn\'t work.

My code:

WebBrowser browser = new WebBrowser();
browser.Navigate(\"         


        
5条回答
  •  无人及你
    2021-01-07 02:29

    The WebBrowser isn't going to do it's job until the current thread finishes it's work, if you changed it to be something like this:

            WebBrowser browser = new WebBrowser();
            browser.Navigate("http://www.google.com");
            browser.Navigated += (s, e) =>
                {
                    var html = browser.DocumentText;
                };
    

    The variable will be set.

    But, as others have mentioned, the document completed is a better event to attach to, as at that time, the entire document will be completed (appropriate name!)

            WebBrowser browser = new WebBrowser();
            browser.Navigate("http://www.google.com");
    
            browser.DocumentCompleted += (s, e) =>
                {
                    var html = browser.DocumentText;
                    html.ToString();
                };
    

提交回复
热议问题