WebBrowser - empty DocumentText

前端 未结 5 1071
粉色の甜心
粉色の甜心 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:28

    You should use DocumentCompleted event, and if you don't have WebForms application, also ApplicationContext might be needed.

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Context ctx = new Context();
            Application.Run(ctx);
    
            // ctx.Html; -- your html
        }
    }
    
    class Context : ApplicationContext
    {
        public string Html { get; set; }
    
        public Context()
        {
            WebBrowser browser = new WebBrowser();
            browser.AllowNavigation = true;
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
            browser.Navigate("http://www.google.com");
        }
    
        void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Html = ((WebBrowser)sender).DocumentText;
            this.ExitThread();
        }
    }
    

提交回复
热议问题