WebBrowser - empty DocumentText

萝らか妹 提交于 2019-12-19 04:11:45

问题


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

My code:

WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");

while(browser.DocumentText == "")
{
    continue;
}
string html = browser.DocumentText;

browser.DocumentText is always "". Why?


回答1:


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();
            };



回答2:


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();
    }
}



回答3:


Attach to the DocumentCompleted event, the code is as below

browser.DocumentCompleted += (s, e) =>
{
    string html = browser.DocumentText;
};



回答4:


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;

}



回答5:


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



来源:https://stackoverflow.com/questions/8526161/webbrowser-empty-documenttext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!