WebBrowser - empty DocumentText

前端 未结 5 1022
粉色の甜心
粉色の甜心 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();
        }
    }
    
    0 讨论(0)
  • 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();
                };
    
    0 讨论(0)
  • 2021-01-07 02:39

    Try something like this

    string url = string.Empty:
    string html = "http://www.google.com/";
    string url = html;
    if (!url.StartsWith("http://") && !url.StartsWith("https://"))
    {
       url = "http://" + url;
    }
    browser.Navigate(new Uri(url)); 
    

    replace it within your While loop where necessary

    0 讨论(0)
  • 2021-01-07 02:42

    If you need the DocumentText you should handle the DocumentCompleted event

      browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
    

    See event below

    void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
    
            WebBrowser wb = (WebBrowser)sender;
            string text = wb.DocumentText;
    
    }
    
    0 讨论(0)
  • 2021-01-07 02:45

    Attach to the DocumentCompleted event, the code is as below

    browser.DocumentCompleted += (s, e) =>
    {
        string html = browser.DocumentText;
    };
    
    0 讨论(0)
提交回复
热议问题