【问题标题】:Web browser control not working to get textWeb 浏览器控件无法获取文本
【发布时间】:2013-02-04 09:22:58
【问题描述】:

我正在尝试获取网页的源代码并将其保存到 Richtextbox,并且该 Web 浏览器控件导航到新 URL。但我得到一个空白的富文本框。这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        webBrowser2.Navigate("http://www.gmail.com");
    }
private void timer1_Tick(object sender, EventArgs e)
    {
        if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
        {
            timer1.Enabled = false;
            richTextBox1.Text = webBrowser2.DocumentText;
            webBrowser2.Navigate("new URL");
        }
    }

【问题讨论】:

标签: c# .net webbrowser-control


【解决方案1】:

我发现你正在做的事情有两种选择。

1) 使用DocumentCompleted:

private void Form_Load(object sender, EventArgs e) {
    webBrowser.Navigate("www.google.com");
}

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    richTextBox.Text = webBrowser.DocumentText;
}

2) 使用WebClient:

private void Form_Load(object sender, EventArgs e) {
    using (System.Net.WebClient wc = new System.Net.WebClient()) {
        richTextBox.Text = wc.DownloadString("http://www.google.com");
    }
}

【讨论】:

  • Wihitehead 我已经尝试了第一种方法,但是richtextbox 仍然是空白的,并且新的 URL 也没有使用?
  • 您需要为自己做一些工作。我省略了订阅活动,之后导航等各种事情。您需要填写与您相关的空白。
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
相关资源
最近更新 更多